Detect Users' Browser Capabilities with PHP

Discussion in 'Web Development' started by pradeep, May 31, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    In today's internet of JavaScript rich UI, it's sometimes very important to know the browser/platform user is using for browsing your site so that you can change the content dynamically according to the browser or the platform. Many sites are already working with this, like Firefox's official website automatically takes you to Linux downloads if you are using a browser on Linux OS, also nowadays browsers are evolving very fast and at times the browser version too becomes important, like a page that works well on IE7 does not look the same on IE6 or say Google Chrome/Opera.

    Here we'll see how you can get a lot of information about the current browser using PHP function get_browser().

    Checking If Available



    get_browser() has been available since PHP 4, but sometimes it might on work because you'll see to set the path for browscap.ini in PHP's configuration file i.e. php.ini. In case you do not have browscap.ini on your system download it from http://browsers.garykeith.com/downloads.asp

    Usage



    Usage is pretty simple, the function get_browser ([ string $user_agent [, bool $return_array = false ]] ) you may pass null for the first argument, the function will automatically get the useragent from environment variables, and for the second parameter it is optional too, pass true if you want the function to return data in an array instead of an object.

    The function will return key-value pairs for various features, browser name, version etc. The sample code below and the output on various browsers will give you an idea.

    PHP:
    <?php
    $browser 
    get_browser(nulltrue);

    print 
    '<pre>';
    print 
    $_SERVER['HTTP_USER_AGENT'] . "\n\n";
    print_r($browser);
    print 
    '</pre>';
    ?>
    Output on Lynx (CLI browser):
    Code:
    Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.8e-fips-rhel5
    
    Array
    (
        [browser_name_regex] => ^lynx/2\.8.*$
        [browser_name_pattern] => Lynx/2.8*
        [parent] => Lynx
        [version] => 2.8
        [majorver] => 2
        [minorver] => 8
        [comment] => Lynx
        [browser] => Lynx
        [frames] => 1
        [iframes] => 1
        [tables] => 1
        [platform] => unknown
        [platform_version] => unknown
        [alpha] =>
        [beta] =>
        [win16] =>
        [win32] =>
        [win64] =>
        [cookies] =>
        [backgroundsounds] =>
        [javascript] =>
        [vbscript] =>
        [javaapplets] =>
        [activexcontrols] =>
        [ismobiledevice] =>
        [issyndicationreader] =>
        [crawler] =>
        [cssversion] => 0
        [aolversion] => 0
    )
    
    Output on Firefox:
    Code:
    Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
    
    Array
    (
        [browser_name_regex] => ^mozilla/5\.0 (.*windows nt 5\.1.*rv:.*) gecko/.* firefox/10\..*$
        [browser_name_pattern] => Mozilla/5.0 (*Windows NT 5.1*rv:*) Gecko/* Firefox/10.*
        [parent] => Firefox 10.0
        [platform] => WinXP
        [platform_version] => 5.1
        [comment] => Firefox 10.0
        [browser] => Firefox
        [version] => 10.0
        [majorver] => 10
        [minorver] => 0
        [win32] => 1
        [frames] => 1
        [iframes] => 1
        [tables] => 1
        [cookies] => 1
        [javascript] => 1
        [javaapplets] => 1
        [cssversion] => 3
        [alpha] => 
        [beta] => 
        [win16] => 
        [win64] => 
        [backgroundsounds] => 
        [vbscript] => 
        [activexcontrols] => 
        [ismobiledevice] => 
        [issyndicationreader] => 
        [crawler] => 
        [aolversion] => 0
    )
    
    Output on IE:
    Code:
    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR  2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR  3.5.30729)
    
    Array
    (
        [browser_name_regex] => ^mozilla/4\.0 (compatible; msie 8\.0; .*windows nt 5\.1.*; trident/4\.0.*).*$
        [browser_name_pattern] => Mozilla/4.0 (compatible; MSIE 8.0; *Windows NT 5.1*; Trident/4.0*)*
        [parent] => IE 8.0
        [platform] => WinXP
        [platform_version] => 5.1
        [comment] => IE 8.0
        [browser] => IE
        [version] => 8.0
        [majorver] => 8
        [minorver] => 0
        [win32] => 1
        [frames] => 1
        [iframes] => 1
        [tables] => 1
        [cookies] => 1
        [backgroundsounds] => 1
        [javascript] => 1
        [vbscript] => 1
        [javaapplets] => 1
        [activexcontrols] => 1
        [cssversion] => 2
        [alpha] => 
        [beta] => 
        [win16] => 
        [win64] => 
        [ismobiledevice] => 
        [issyndicationreader] => 
        [crawler] => 
        [aolversion] => 0
    )
    
    Output on Google Chrome:
    Code:
    Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
    
    Array
    (
        [browser_name_regex] => ^mozilla/5\.0 (.*windows nt 5\.1.*) applewebkit/.* (khtml, like gecko).*chrome/19\..*safari/.*$
        [browser_name_pattern] => Mozilla/5.0 (*Windows NT 5.1*) AppleWebKit/* (KHTML, like Gecko)*Chrome/19.*Safari/*
        [parent] => Chrome 19.0
        [platform] => WinXP
        [platform_version] => 5.1
        [comment] => Chrome 19.0
        [browser] => Chrome
        [version] => 19.0
        [majorver] => 19
        [minorver] => 0
        [win32] => 1
        [frames] => 1
        [iframes] => 1
        [tables] => 1
        [cookies] => 1
        [javascript] => 1
        [javaapplets] => 1
        [cssversion] => 3
        [alpha] => 
        [beta] => 
        [win16] => 
        [win64] => 
        [backgroundsounds] => 
        [vbscript] => 
        [activexcontrols] => 
        [ismobiledevice] => 
        [issyndicationreader] => 
        [crawler] => 
        [aolversion] => 0
    )
    

    References



    http://php.net/manual/en/function.get-browser.php
    http://browsers.garykeith.com/downloads.asp
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice