Site Loader
Get a Quote

Identify browser & Platform

Identify browser & Platform

Identify browser & Platform: In this session, we will discuss how you get information about the browser and know about operating system platform.

Identifying Browser & Platform in PHP

There is a phpinfo.php file in PHP environment setup which contains global environment access variable .which we use to get information from the environment.

HTTP_USER_AGENT which gives info about user’s browser and operating system which contains in the phpinfo.php.

PHP5.0 contains a function getenv() to access the value of all the environment variables. The information contained in the HTTP_USER_AGENT environment variable can be used to create dynamic content appropriate to the browser.

In Below example, we demonstrate how you can know a client browser and operating system.

NOTE − The function preg_match()is a function  used  to match the string  in another string variable.

<html>

   <body>

   <title>
Identifying Browser & Platform in php
</title>

      <?php

        



         function getBrowserDetails() {

            $user_agent = $_SERVER['HTTP_USER_AGENT'];

            $browsername = NA;

            $osplatform = NA;

            $browserversion = "";


  //First  weill get the operating system platform?


            

            if (preg_match('/linux/i', $user_agent)) {

               $osplatform = 'linux';

            }elseif (preg_match('/macintosh|mac os x/i', $user_agent)) {

               $osplatform = 'mac';

            }elseif (preg_match('/windows|win32/i', $user_agent)) {

               $osplatform = 'windows';

            }

           
 // Next get the browser name of the useragent 
           

            if(preg_match('/MSIE/i',$user_agent) && !preg_match('/Opera/i',$user_agent)) {

               $browsername = 'Internet Explorer';

               $ub = "MSIE";

            } elseif(preg_match('/Firefox/i',$user_agent)) {

               $browsername = 'Mozilla Firefox';

               $ub = "Firefox";

            } elseif(preg_match('/Chrome/i',$user_agent)) {

               $browsername = 'Google Chrome';

               $ub = "Chrome";

            }elseif(preg_match('/Safari/i',$user_agent)) {

               $browsername = 'Apple Safari';

               $ub = "Safari";

            }elseif(preg_match('/Opera/i',$user_agent)) {

               $browsername = 'Opera';

               $ub = "Opera";

            }elseif(preg_match('/Netscape/i',$user_agent)) {

               $browsername = 'Netscape';

               $ub = "Netscape";

            }

           
// finally know  the correct browserversion of browser
          

            $known = array('browserversion', $ub, 'other');

            $pattern = '#(?' . join('|', $known) . ')[/ ]+(?[0-9.|a-zA-Z.]*)#';

           

            if (!preg_match_all($pattern, $user_agent, $matches)) {

               // we have no matching number just continue

            }

           

         

            $i = count($matches['browser']);

           

            if ($i != 1) {

               

               if (strripos($user_agent,"browserversion") < strripos($user_agent,$ub)){ $browserversion= $matches['browserversion'][0]; }else { $browserversion= $matches['browserversion'][1]; } }else { $browserversion= $matches['browserversion'][0]; } if ($browserversion == null || $browserversion == "") { $browserversion = "?"; } return array( 'userAgent' => $user_agent,

               'browsername'      => $browsername,

               'browserversion'   => $browserversion,

               'platform'  => $osplatform,

               'pattern'   => $pattern

            );

         }
		 

        

         
 // now call function
         $ua = getBrowserDetails();

         $browserdetails = "Your browser details: " . $ua['browsername'] . " " . $ua['browserversion'] .

            " on " .$ua['osplatform'] . " reports: 
" . $ua['userAgent'];

        

         print_r($browserdetails);

     
        

      ?>

  

   </body>

</html>

This is producing following result on my machine. This result may be different for your computer depending on what you are using.

It will produce the following result −

Your browser: Google Chrome 54.0.2840.99 on windows reports:

Mozilla/5.1 (Windows NT 6.3; Win32; x32) AppleWebKit/537.36 (KHTML, like Gecko)

   Chrome/54.0.2840.99 Safari/537.36

 

 

 

Here session about Identifying Browser & Platform in PHP ends

 

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.