Recent comments

You are here

How to detect browser type

So I needed to detect the browser type so that I could use a different css based on the rowser the visitor of the site is using. This allows customized formating so that different browsers will output the screen correctly. While searching for a way to do this I came across many many articles that were out of date or simply didn't work! Finally I decided to use one I found over at phpclasses.org However it didnt work the way it was but with some slight modifications I have now got a working script that

Detects multiple browser types
Gets the version number
displays either the major version or the major/minor version
Is easily upgraded to display a different css for each

Both the files are available below to download.For each page just use the following command in your php file

include_once('checkbrowser.php');

make sure of course that you have uploaded both the files to the same directory as the files you are including it in. If the files exist in a different directory then change the include_once() function to point to the correct file location

Now lets look at the files

checkbrowser.php


whatBrowser());
?>

None of this needs to be customized unless you change the file name of the file named browser_class_inc.php which is not needed so I recomend you leave it named the same.

browser_class_inc.php


useragent = $agent;
	}
	

	/**
	 * Method to test for Opera
	 * @param void
	 * @return property $broswer
	 * @return property version
	 * @return bool false on failure
	 */
	function isOpera()
	{
		// test for Opera		
		if (eregi("opera",$this->useragent))
		{
			$val = stristr($this->useragent, "opera");
			if (eregi("/", $val)){
				$val = explode("/",$val);
				$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
			}else{
				$val = explode(" ",stristr($val,"opera"));
				$this->browsertype = $val[0];
				$this->version = $val[1];
			}
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to check for FireFox
	 * @param void
	 * @return bool false on failure
	 */ 
	function isFirefox()
	{
		if(eregi("Firefox", $this->useragent))
		{
			$this->browsertype = "Firefox"; 
			$val = stristr($this->useragent, "Firefox");
			$val = explode("/",$val);
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}
	function isChrome()
	{
		if(eregi("Chrome", $this->useragent))
		{
			$this->browsertype = "Chrome"; 
			$val = stristr($this->useragent, "Chrome");
			$val = explode("/",$val);
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to check for Konquerer
	 * @param void
	 * @return prop $browser
	 * @return prop $version
	 * @return bool true on success
	 */
	function isKonqueror()
	{
		if(eregi("Konqueror",$this->useragent))
		{
			$val = explode(" ",stristr($this->useragent,"Konqueror"));
			$val = explode("/",$val[0]);
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
		
	}//end func
	
	/**
	 * Method to check for Internet Explorer v1
	 * @param void
	 * @return bool true on success
	 * @return prop $browsertype
	 * @return prop $version
	 */
	function isIEv1()
	{
		if(eregi("microsoft internet explorer", $this->useragent))
		{
			$this->browsertype = "MSIE"; 
			$this->version = "1.0";
			$var = stristr($this->useragent, "/");
			if (ereg("308|425|426|474|0b1", $var))
			{
				$this->version = "1.5";
			}
			return TRUE;
		}
		else {
			return FALSE;
		}
	}//end function
	
	/**
	 * Method to check for Internet Explorer later than v1
	 * @param void
	 * @return bool true on success
	 * @return prop $browsertype
	 * @return prop $version
	 */
	function isMSIE()
	{
		if(eregi("msie", $this->useragent) && !eregi("opera",$this->useragent))
		{
			$this->browsertype = "MSIE"; 
			$val = explode(" ",stristr($this->useragent,"msie"));
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
			


		}
		else {
			return FALSE;
		}
	}//end function
	
	/**
	 * Method to check for Galeon
	 * @param void
	 * @return bool true on success
	 */
	function isGaleon()
	{
		if(eregi("galeon",$this->useragent))
		{
			$val = explode(" ",stristr($this->useragent,"galeon"));
			$val = explode("/",$val[0]);
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}//end func
	
	/**
	 * Now we do the tests for browsers I can't test...
	 * If someone finds a bug, please report it ASAP to me please!
	 */
	
	/**
	 * Method to check for WebTV browser
	 * @param void
	 * @return bool true on success
	 * @return prop $browsertype
	 * @return prop $version
	 */
	function isWebTV()
	{
		if(eregi("webtv",$this->useragent))
		{
			$val = explode("/",stristr($this->useragent,"webtv"));
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}
	


	/**
	 * Method to test for iCab
	 * @param void
	 * @return bool true on success
	 */

	function isIcab()
	{
		if(eregi("icab",$this->useragent))
		{
			$val = explode(" ",stristr($this->useragent,"icab"));
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}

		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to test for the OmniWeb Browser
	 * @param void
	 * @return bool True on success
	 */

	function isOmniWeb()
	{
		if(eregi("omniweb",$this->useragent))
		{
			$val = explode("/",stristr($this->useragent,"omniweb"));
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}

		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to check for Phoenix Browser
	 * @param void
	 * @return bool true on success
	 */


	function isPhoenix()
	{
		if(eregi("Phoenix", $this->useragent))
		{
			$this->browsertype = "Phoenix"; 
			$val = explode("/", stristr($this->useragent,"Phoenix/"));
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to check for Firebird
	 * @param void
	 * @return bool true on success
	 */

	function isFirebird()
	{
		if(eregi("firebird", $this->useragent))
		{
			$this->browsertype = "Firebird"; 
			$val = stristr($this->useragent, "Firebird");
			$val = explode("/",$val);
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to check for Mozilla alpha/beta
	 * @param void
	 * @return bool true on success
	 */

	function isMozAlphaBeta()
	{
		if(eregi("mozilla",$this->useragent) && 
		   eregi("rv:[0-9].[0-9][a-b]",$this->useragent) && 
		   !eregi("netscape",$this->useragent))
		
		{
			$this->browsertype = "Mozilla"; 
			$val = explode(" ",stristr($this->useragent,"rv:"));
			eregi("rv:[0-9].[0-9][a-b]",$this->useragent,$val);
			$majver = explode(".", $val[1]);
			$this->version = str_replace("rv:","",$majver[0]);
			return TRUE;
		}
		else {
			return FALSE;
		}
	}//end function

	/**
	 * Method to check for Mozilla Stable versions
	 * @param void
	 * @return bool true on success
	 */

	function isMozStable()
	{
		if(eregi("mozilla",$this->useragent) &&
		   eregi("rv:[0-9]\.[0-9]",$this->useragent) && 
		   !eregi("netscape",$this->useragent))
		{
			$this->browsertype = "Mozilla"; 
			$val = explode(" ",stristr($this->useragent,"rv:"));
			eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent,$val);
			$majver = explode(".", $val[1]);
			$this->version = str_replace("rv:","",$majver[0]);

		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Method to check for Lynx and Amaya
	 * @param void
	 * @return bool true on success
	 */

	function isLynx()
	{
		if(eregi("libwww", $this->useragent))
		{
			if (eregi("amaya", $this->useragent))
			{
				$val = explode("/",stristr($this->useragent,"amaya"));
				$this->browsertype = "Amaya"; 
				$val = explode(" ", $val[1]);
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
			} else {
				$val = explode("/",$this->useragent);
				$this->browsertype = "Lynx"; 
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
			}
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * method to check for safari browser
	 * @param void
	 * @return bool true on success
	 */

	function isSafari()
	{
		if(eregi("Safari", $this->useragent))
		{
			$this->browsertype = "Safari"; 
			$val = stristr($this->useragent, "Safari");
			$val = explode("/",$val);
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}	/**
	 * Various tests for Netscrape
	 * @param void
	 * @return bool true on success
	 */
	
       function isNetscape()
	{
		if(eregi("netscape",$this->useragent))
		{
			$val = explode(" ",stristr($this->useragent,"netscape"));
			$val = explode("/",$val[0]);
			$this->browsertype = $val[0];
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		elseif(eregi("mozilla",$this->useragent) && 
				!eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent))
		{
			$val = explode(" ",stristr($this->useragent,"mozilla"));
			$val = explode("/",$val[0]);
			$this->browsertype = "Netscape"; 
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}//end func
	
	/**
	 * Method to check for AOL connections
	 * @param void
	 * @return bool true on Success
	 */

	function isAOL()
	{
		if (eregi("AOL", $this->useragent)){
			$var = stristr($this->useragent, "AOL");
			$var = explode(" ", $var);
			$this->aol = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
			return TRUE;
		}
		else { 
			return FALSE;
		}
	}
	
	/**
	 * Method to tie them all up and output something useful
	 * @param void
	 * @return array
	 */
	function whatBrowser()
	{
		
		$this->isOpera();
		$this->isFirefox();
		$this->isKonqueror();
		$this->isIEv1();
		$this->isMSIE();
		$this->isGaleon();
		$this->isIcab();
		$this->isOmniWeb();
		$this->isPhoenix();
		$this->isFirebird();
		$this->isLynx();
		$this->isSafari();
              $this->isChrome();
		//$this->isMozAlphaBeta();
		//$this->isMozStable();
		//$this->isNetscape();
		$this->isAOL();
		return array('browsertype' => $this->browsertype, 
					 'version' => $this->version,); 
	}
}//end class


?>

Now lets take a look at how we can modify this to do some different things

Lets look at a basic function to check for the browser. each function looks like this:

function isFirefox()
	{
		if(eregi("Firefox", $this->useragent))
		{
			$this->browsertype = "Firefox"; 
			$val = stristr($this->useragent, "Firefox");
			$val = explode("/",$val);
			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];
		}
		else {
			return FALSE;
		}
	}


This function checks if it is firefox. It returns only the major version number. To get it to return a fill version number we delete these lines:

			$majver = explode(".", $val[1]);
                     $this->version = $majver[0];

and put this line in the same place

                     $this->version = $val[1];

This now echos the browser type followed by the full version number

Ok but how do we get it to go to a different css for each browser type? Actually we need to check the version number also because different versions of the same browser may need different css's to output correctly so we do change the checkbrowser.php file here is how

whatBrowser();
$final = $thisbrowser['browsertype'];
$final .= " ";
$final .= $thisbrowser['version'];

echo $final;

//Finally we can tell it what to do with the browser type  and version

if($final == 'MSIE 8'){
include('IE8fixes.css');
}

if($final == 'Firefox 3'){
include('firefox3fixes.css');
}
?>

Now in this code we can check the variable $final and include the correct css file based on the browser and version number returned. The code we add to do this will look like this


if($final == 'MSIE 8'){
include('IE8fixes.css');
}

We must add one of these for each browser type followed by the version number if we dont the page will be parsed without including the css so we only really need to do this if we have css fixes to make so that a page will be correctly formatted by a certain browser. We can change the name of the css file or add a path to it if needed by changing the include() function to match the reality of your needs.

I hope this tutorial helps people be able to detect different browsers and create more browser-compatible websites
If you have questions or comments let me know Or if you cant get it working Ill help you out if you leave a comment.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer