Programming Tips - PHP: Detect which browser the user is running

Date: 2010aug12 Language: php Keywords: agent Q. PHP: Detect which browser the user is running A. Please note that its best to use feature detection rather than browser detection. Maybe the next version of IE will have that feature you assume it doesn't have. Here is some simple php code that checks to see if a particular browser is in use. This is what you usually want.
function isFirefox() { return strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE; }
function isIE() { return strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE; }
function isAndroid() { return strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== FALSE; }
php has a function to take apart the HTTP_USER_AGENT string: http://ca.php.net/manual/en/function.get-browser.php But this is usually not necessary and (I have read) it can be slow.