Friday, March 4, 2011

Retrive browser and OS in PHP

Hi friends,

I need to retrive the browser and OS through php coding.

I had used $_SERVER['HTTP_USER_AGENT'], but its shows the following

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; IEMB3)

How can i separate the browser and Os from the above received value..

Please guide me..

Thanks in advance

From stackoverflow
  • Grab a copy of browscap.ini compare $_SERVER['HTTP_USER_AGENT'] with that file.

  • Hi, you can use simple explode();

    <?php
    $ex=explode(' ',$_SERVER['HTTP_USER_AGENT']);
    echo 'OS: '.$ex[4].' '.$ex[5].' '.$ex[6].'/n'; 
    echo 'Browser: '.$ex[0]; 
    ?>
    
  • Just use the built in function for this http://ie.php.net/manual/en/function.get-browser.php

  • You could use the Google code Browscap class. It essentially does the same thing as PHP's get_browser(), but you don't have to worry about keeping your browscap.ini up to date.

    This worked for me:

    require('Browscap.php');
    $browscap = new Browscap('/path/to/cache');
    var_dump($browscap->getBrowser());
    
  • The best way would be using the buil-in get_browser() function as it's a few times faster than the Google-Code-version if you're only running it once. (If you're running it 4+ times the Google-Code-version is faster)

    So unless you need the auto-update and only use one check at a time, you should use the built-in version. :)

    And it shouldn't be that hard to make a cronjob to get the newest version. ;)

0 comments:

Post a Comment