How to Identify the browser and platform using php

This post helps to identify your browser and your Platform using php code. PHP have some environment variable and functions, HTTP_USER_AGENT is a one of the environment variable is used to identify your browser and your platform. getenv() is a function used to allow the environment variable using this type of environment variables we can easily find out our browser and platform using PHP. This is a simple method you can try this.


Code:

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>How to Identify the browser and Platform</title>
</head>
<body>
<?php
   $viewer = getenv( "HTTP_USER_AGENT" );
   $browser = "An unidentified browser";
   if( preg_match( "/MSIE/i", "$viewer" ) )
   {
      $browser = "Internet Explorer";
   }
   
   else if(  preg_match( "/Netscape/i", "$viewer" ) )
   {
      $browser = "Netscape";
   }
   
   elseif(preg_match('/Chrome/i',"$viewer"))
   {
$browser="Google Chrome" ;
   }
   
   else if(  preg_match( "/Mozilla/i", "$viewer" ) )
   {
      $browser = "Mozilla";
   }
   $platform = "An unidentified OS!";
   if( preg_match( "/Windows/i", "$viewer" ) )
   {
      $platform = "Windows!";
   }
   else if ( preg_match( "/Linux/i", "$viewer" ) )
   {
      $platform = "Linux!";
   }
   echo  "<h2>You are using $browser on $platform</h2>" ;
?>
</body>
</html>


Post a comment

0 Comments