codescracker


php

PHP Web Developments



« Previous Tutorial Next Tutorial »


Identify Browser and Platform using PHP

Use a variable available in PHP, named HTTP_USER_AGENT, to identify the user's browser and operating system. Let's look at the following example which demonstrates how you can identify a client browser and operating system. This example will display the full information of your browser and windows along with version:

<html>
<body>

<?php
	function getBrowser()
	{
		$u_agent = $_SERVER['HTTP_USER_AGENT'];
		$bname = 'Unknown';
		$platform = 'Unknown';
		$version= "";
	
		if(preg_match('/linux/i', $u_agent))
		{
			$platform = 'linux';
		}	
		elseif(preg_match('/macintosh|mac os x/i', $u_agent))
		{
			$platform = 'mac';
		}	
		elseif(preg_match('/windows|win32/i', $u_agent))
		{
			$platform = 'windows';
		}	
	
		if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
		{
			$bname = 'Internet Explorer';
			$ub = "MSIE";
		}
		elseif(preg_match('/Firefox/i',$u_agent))
		{
			$bname = 'Mozilla Firefox';
			$ub = "Firefox";
		}	
		elseif(preg_match('/Chrome/i',$u_agent))
		{
			$bname = 'Google Chrome';
			$ub = "Chrome";
		}	
		elseif(preg_match('/Safari/i',$u_agent))
		{
			$bname = 'Apple Safari';
			$ub = "Safari";
		}
		elseif(preg_match('/Opera/i',$u_agent))
		{
			$bname = 'Opera';
			$ub = "Opera";
		}	
		elseif(preg_match('/Netscape/i',$u_agent))
		{
			$bname = 'Netscape';
			$ub = "Netscape";
		}
	
		$known = array('Version', $ub, 'other');
		$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
		if(!preg_match_all($pattern, $u_agent, $matches))
		{
		}
	
		$i = count($matches['browser']);
		if($i != 1)
		{
			if(strripos($u_agent,"Version") < strripos($u_agent,$ub))
			{
				$version= $matches['version'][0];
			}	
			else
			{
				$version= $matches['version'][1];
			}	
		}
		else
		{
			$version= $matches['version'][0];
		}	
	
		if($version==null || $version=="") {$version="?";}
	
		return array(
			'userAgent' => $u_agent,
			'name'      => $bname,
			'version'   => $version,
			'platform'  => $platform,
			'pattern'    => $pattern );
	}

	$ua=getBrowser();
	$yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " . $ua['platform'] . " reports: <br >" . $ua['userAgent'];
	print_r($yourbrowser);
?>

</body>
</html>

It will produce the result which tells about your browser name and version with your platform name and other details

Browser Redirection using PHP

The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. After calling this function the exit() function can be used to halt parsing of rest of the code

Let's look at the following example which demonstrates how you can redirect a browser request to another web page

<html>
<body>
<?php
  if( $_POST["location"] )
  {
     $location = $_POST["location"];
     header( "Location:$location" );
     exit();
  }
?>
   <p>Choose Topic To Learn :</p>
   <form action="<?php $_PHP_SELF ?>" method="POST" target="_blank">
   <select name="location">
      <option value="http://codescracker.com/html">
            HTML Tutorial
      </option>
      <option value="http://codescracker.com/java">
            Java Tutorial
      </option>
      <option value="http://codescracker.com/c">
            C Tutorial
      </option>
      <option value="http://codescracker.com/css">
            CSS Tutorial
      </option>
      <option value="http://codescracker.com/js">
            JavaScript Tutorial
      </option>
      <option value="http://codescracker.com/sql">
            SQL Tutorial
      </option>
   </select>
   <input type="submit" />
   </form>
</body>
</html>

It will display the result that will ask to select one of the following option, for detail try it with your own


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test