Detecting browsers and using browser specific code using PHP

If you’ve ever wanted to attach browser-specific style sheets or javascript into your website without letting the end-user know, javascript may not be the best method. If you’re using a PHP server, here’s a quick and very useful method to achieve this:

<?php if (strpos($_SERVER['HTTP_USER_AGENT'],'Safari') != true) { ?>//If your browser is Safari, this code will not appear.<?php } ?>

The code used above is to prevent code from appearing on Safari, you can just change ‘Safari’ to 'Firefox’ or 'IE’, depending on what you need and change the check method like you would in any programming language.

The comment text in italics may be replaced by the code you want to display(or not display) on a specific browser. If the text-editor you use to edit PHP has formatting options, be sure to remove all formatting. I’ve implemented this technique over here to prevent ajax from taking over in Safari as there’s a conflict between my theme and one of the javascript libraries used on Safari.