Dave's Brain

Browse - Programming Tips - How do I detect which browser the user is running?

Date: 2010aug12
Language: php
Level: beginner
Keywords: agent

Q.  How do I detect which browser the user is running?


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.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.