Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Check/Get Visitors Browser/Operating Systems details in PHP

Hello Friends,

There are plenty of browsers(Mozilla Firefox , Internet Explorer , Google Crome , Safari , Opera and many more) and Operating systems (Windows , Linux , Unix and many more) available in web world. It might be a case when you want to get browser/operating system details of visitors or users who came across your website built up with PHP and fill up a contact request form. Browser/Operating System details will help you to analyze the contact us request made by visitor or user. By looking at browser info & Operating system info,you have a brief idea about users before replying them.

If you want to track visitor's or user's browser details than you may use get_browser() PHP function which is a very easy to remember also. But here i am sharing a function from which you will get a filtered data(only useful details). Below is the function and answer of all those people who want to know "How to get Browser/Operating system details in PHP".

function check_browser()
{
      $browsers = "mozilla msie gecko firefox ";
      $browsers.= "konqueror safari netscape navigator ";
      $browsers.= "opera mosaic lynx amaya omniweb";
      $browsers = split(" ", $browsers);
      $userAgent = strtolower( $_SERVER['HTTP_USER_AGENT']);
      $l = strlen($userAgent);
      for ($i=0; $i
      {
            $browser = $browsers[$i];
            $n = stristr($userAgent, $browser);
            if(strlen($n)>0)
            {
                  $version = "";
                  $navigator = $browser;
                  $j=strpos($userAgent, $navigator)+$n+strlen($navigator)+1;
                  for (; $j<=$l; $j++)
                  {
                         $s = substr ($userAgent, $j, 1);
                         if(is_numeric($version.$s) )
                               $version .= $s;
                         else
                               break;
                  }
            }
       }
       if (strpos($userAgent, 'linux')) {
            $platform = 'linux';
       }
       else if (strpos($userAgent, 'macintosh') || strpos($userAgent, 'mac platform x')) {
            $platform = 'mac';
       }
       else if (strpos($userAgent, 'windows') || strpos($userAgent, 'win32')) {
            $platform = 'windows';
       }
       return array(
            "browser" => $navigator,
            "version" => $version,
            "platform" => $platform,
            "userAgent" => $userAgent);
 }

 $browserdata = check_browser();
 $browser_with_version = $browserdata['browser'].' '.$browserdata['version'];
 $operating_system = $browserdata['platform'];

You need to call simply check_browser() and you will get the specific data of browser details with version and operating system details. Comment below if you find any problem.

To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database and Open-source, enter your email address below. We will send you free tutorials.

Enter your email address:
 
Feel free to ask any question. Just leave your comment below and we will answer your comment with in 24 hours.
Share:

4 Comments

  • Check/Get Visitors Browser/Operating Systems details in PHP…

    There are plenty of browsers(Mozilla Firefox , Internet Explorer , Google Crome , Safari , Opera and many more) and Operating systems (Windows , Linux , Unix and many more) available in web world. It might be a case when you want to get browser/operati…

  • Vivek

    Hi Rakshit,

    Thanks for sharing the wonderful piece of code. It works great but probably unfortunately got missed for Chrome and also for Safari it gives wrong version.

    I have modified it to work for these two also as following:

    0)
    {
    $version = “”;
    $navigator = $browser;
    $j=strpos($userAgent, $navigator)+$n+strlen($navigator)+1;
    for (; $j0){
    break;
    }

    if($browser==’safari’ && strlen($n)>0){
    $pos = strpos($userAgent,$browser);
    if($pos>6){
    $version_pos = $pos-6;
    $version = substr($userAgent,$version_pos,5);
    }
    }

    }
    if (strpos($userAgent, ‘linux’)) {
    $platform = ‘linux’;
    }
    else if (strpos($userAgent, ‘macintosh’) || strpos($userAgent, ‘mac platform x’)) {
    $platform = ‘mac’;
    }
    else if (strpos($userAgent, ‘windows’) || strpos($userAgent, ‘win32’)) {
    $platform = ‘windows’;
    }

    return array(
    “browser” => $navigator,
    “version” => $version,
    “platform” => $platform,
    “userAgent” => $userAgent);
    }

    $browserdata = check_browser();
    $browser_with_version = $browserdata[‘browser’].’ ‘.$browserdata[‘version’];
    $operating_system = $browserdata[‘platform’];

    ?>

  • It’s not works for me can you please help me.

    I am getting the following error
    Parse error: parse error in C:\wamp\www\enc-dec-with-key\user_agent_via_php.php on line 11

  • Watch CSI: Miami – Season 10, Episode 9: A Few Dead Men…

    […]Check/Get Visitors Browser/Operating Systems details in PHP | Programming Facts -PHP Freelancer-Indian Freelancer[…]…

Leave a Reply

Your email address will not be published.