Sooner or later it might become necessary to write an application that tracks site visitor browser types. Here’s some code to use:
This will output something that looks like one of the following:
Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC)
Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0
Reference
A more complete list of USER AGENT strings (tab delimited)
Next, we’ll build a simple browser checker to check for Internet Explorer (IE), Firefox, Opera and Safari:
$useragent = $_SERVER[‘HTTP_USER_AGENT’]);
if (preg_match(‘|MSIE ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘IE’;
} elseif (preg_match( ‘|Opera ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘Opera’;
} elseif(preg_match(‘|Firefox/([0-9\.]+)|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘Firefox’;
} elseif(preg_match(‘|Safari/([0-9\.]+)|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘Safari’;
} else {
// browser not recognized!
$browser_version = 0;
$browser= ‘other’;
}
print “browser: $browser $browser_version”;
?>
Now let’s parse out a simple Operating System (OS) checker using the strstr function:

You should check for Opera before you check for IE since Opera fakes being MSIE.
e.g.:
on October 2nd, 2005 at 7:58 am | #Link CommentOpera 5.12 Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 5.12 [en]
$useragent = $_SERVER[‘HTTP_USER_AGENT’]);
remove the ‘)’ after ‘…GENT’]’ or it won’t work
on July 15th, 2006 at 7:40 am | #Link Commentif you decide to copy&paste the sample, be sure to retype the single quotation marks manualy. those being used on the web-page are not the regular ones
on November 14th, 2007 at 11:15 am | #Link Commentwhen checking for opera you forgat the /
} elseif (preg_match( ‘|Opera ([0-9].[0-9]{1,2})|
becomes
} elseif (preg_match( ‘|Opera/([0-9].[0-9]{1,2})|
on March 31st, 2008 at 6:56 am | #Link CommentHeck yeah, I’m definitely going to give that browser check script a try. Thanks!
on April 29th, 2008 at 3:17 pm | #Link Comment< b >< a href=”http://theoriginofgods.com/JoanUouu” >short haircuts< /a >< /b >< /blockquote >
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how can we communicate?
on February 5th, 2012 at 10:30 pm | #Link CommentTiana Kittrell
Hey, thanks for the article.Thanks Again. Really Great.
on February 6th, 2012 at 4:22 pm | #Link Comment