Browser Detection

I needed a simple PHP class that detect browser and OS based on user agent string. I’ve tried some free available classes on net like PEAR::Net_UserAgent_Detect, but none worked for me. They were either too big or didn’t worked well with most recent browsers (like Opera 8.5 or Netscape 8).

After I spent some time by looking to code that suit my needs, I’ve decided to write my own class which should be working fine with most recent browsers. It recognize most popular browsers (like IE, Firefox, Opera …) as well as it’s version and subversion number (like Opera 8.50, Firefox 1.0.6, etc.). I didn’t care to add support for ancient and no longer used browsers like Netscape 4.x etc.

Here is the class code. You can use it in your projects for free as long as you don’t change header information.

<?php
/**
 * @version $Id$
 * @package dinke.net
 * @copyright © 2005 Dinke.net
 * @author Dragan Dinic 
 */

/**
 * Browser Detection class
 * contains common static method for 
 * getting browser version and os
 *
 * It only support recent and popular browsers
 * and doesn't recognize bots (like google, yahooo etc)
 * for more browser support you might want to try some other class
 *
 * usage
 * <code>
 * $browser = Browser_Detection::get_browser($_SERVER['HTTP_USER_AGENT']);
 * $os = Browser_Detection::get_os($_SERVER['HTTP_USER_AGENT']);
 * </code>
 * @access public
 */
class Browser_Detection
{
	
	/**
	 * Get browsername and version
	 * @param string user agent	 
	 * @return string browser name and version or false if unrecognized
	 * @static 
	 * @access public
	 */
	function get_browser($useragent)
	{
		//check for most popular browsers first
		//unfortunately that's ie. We also ignore opera and netscape 8 
		//because they sometimes send msie agent
		if(strpos($useragent,"MSIE") !== false && strpos($useragent,"Opera") === false && strpos($useragent,"Netscape") === false)
		{
			//deal with IE
			$found = preg_match("/MSIE ([0-9]{1}\.[0-9]{1,2})/",$useragent,$matches);
			if($found)
			{
				return "Internet Explorer " . $matches[1];
			}
		}
		elseif(strpos($useragent,"Gecko"))
		{
			//deal with Gecko based
			
			//if firefox
			$found = preg_match("/Firefox\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Mozilla Firefox " . $matches[1];
			}
			
			//if Netscape (based on gecko)
			$found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Netscape " . $matches[1];
			}
			
			//if Safari (based on gecko)
			$found = preg_match("/Safari\/([0-9]{2,3}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Safari " . $matches[1];
			}
			
			//if Galeon (based on gecko)
			$found = preg_match("/Galeon\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Galeon " . $matches[1];
			}
			
			//if Konqueror (based on gecko)
			$found = preg_match("/Konqueror\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Konqueror " . $matches[1];
			}		

			//no specific Gecko found
			//return generic Gecko
			return "Gecko based";					
		}
		
		elseif(strpos($useragent,"Opera") !== false)
		{
			//deal with Opera
			$found = preg_match("/Opera[\/ ]([0-9]{1}\.[0-9]{1}([0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Opera " . $matches[1];
			}
		}
		elseif (strpos($useragent,"Lynx") !== false)
		{
			//deal with Lynx			
			$found = preg_match("/Lynx\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Lynx " . $matches[1];
			}
			
		}
		elseif (strpos($useragent,"Netscape") !== false)
		{
			//NN8 with IE string
			$found = preg_match("/Netscape\/([0-9]{1}\.[0-9]{1}(\.[0-9])?)/",$useragent,$matches);
			if($found)
			{
				return "Netscape " . $matches[1];
			}
		}
		else 
		{
			//unrecognized, this should be less than 1% of browsers (not counting bots like google etc)!
			return false;
		}
	}
	
	/**
	 * Get browsername and version
	 * @param string user agent	 
	 * @return string os name and version or false in unrecognized os
	 * @static 
	 * @access public
	 */
	function get_os($useragent)
	{		
		$useragent = strtolower($useragent);
		
		//check for (aaargh) most popular first		
		//winxp
		if(strpos("$useragent","windows nt 5.1") !== false)
		{
			return "Windows XP";			
		}
		elseif (strpos("$useragent","windows 98") !== false)
		{
			return "Windows 98";
		}
		elseif (strpos("$useragent","windows nt 5.0") !== false)
		{
			return "Windows 2000";
		}
		elseif (strpos("$useragent","windows nt 5.2") !== false)
		{
			return "Windows 2003 server";
		}
		elseif (strpos("$useragent","windows nt 6.0") !== false)
		{
			return "Windows Vista";
		}
		elseif (strpos("$useragent","windows nt") !== false)
		{
			return "Windows NT";
		}
		elseif (strpos("$useragent","win 9x 4.90") !== false && strpos("$useragent","win me"))
		{
			return "Windows ME";
		}
		elseif (strpos("$useragent","win ce") !== false)
		{
			return "Windows CE";
		}
		elseif (strpos("$useragent","win 9x 4.90") !== false)
		{
			return "Windows ME";
		}
		elseif (strpos("$useragent","mac os x") !== false)
		{
			return "Mac OS X";
		}
		elseif (strpos("$useragent","macintosh") !== false)
		{
			return "Macintosh";
		}
		elseif (strpos("$useragent","linux") !== false)
		{
			return "Linux";
		}
		elseif (strpos("$useragent","freebsd") !== false)
		{
			return "Free BSD";
		}
		elseif (strpos("$useragent","symbian") !== false)
		{
			return "Symbian";
		}
		else 
		{
			return false;
		}
	}
}
?>

Update: Class is updated with support for Windows Vista, Google Chrome and iPhone. You can download latest version from here

3 thoughts to “Browser Detection”

  1. I use your class for years now. Because of the easy handling it makes it a snap to realise things like a browserswitch, user stats etc.
    Any plans on update this class with new OS like Vista or new Browsers like Chrome?
    Thank you for your work!

  2. Hey Andre,

    Just updated class with Windows Vista support as well as Google Chrome and iPhone.

    Thanks,
    D

Comments are closed.