Get browser name and version

Views: 30 Last modified: August 17th, 2011 Comments: 0

Are you curious about the browsers and versions your visitors are using ? This PHP function will get the browser name and version for you.
Compatible with

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera

This function does not require a parameter as it uses the superglobal “SERVER” for gathering information about the HTTP_USER_AGENT.

<?php
/**
 * browser()
 * Returns browser information in a string
 * Compatible with Chrome, Internet Explorer, Firefox, Safari and Opera
 * This settings are according to the superglobal SERVER
 * @return string Name & Version
 */
function browser(){
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $browsers = array(
                        'Chrome' => array('Google Chrome','Chrome/(.*)\s'),
                        'MSIE' => array('Internet Explorer','MSIE\s([0-9\.]*)'),
                        'Firefox' => array('Firefox', 'Firefox/([0-9\.]*)'),
                        'Safari' => array('Safari', 'Version/([0-9\.]*)'),
                        'Opera' => array('Opera', 'Version/([0-9\.]*)')
                        ); 

    $browser_details = array();

        foreach ($browsers as $browser => $browser_info){
            if (preg_match('@'.$browser.'@i', $user_agent)){
                $browser_details['name'] = $browser_info[0];
                    preg_match('@'.$browser_info[1].'@i', $user_agent, $version);
                $browser_details['version'] = $version[1];
                    break;
            } else {
                $browser_details['name'] = 'Unknown';
                $browser_details['version'] = 'Unknown';
            }
        }

    return 'Browser: '.$browser_details['name'].' Version: '.$browser_details['version'];
}

// Example of use
echo browser();
?>
VN:F [1.9.13_1145]
Rating: 9.0/10 (1 vote cast)
VN:F [1.9.13_1145]
Rating: +1 (from 1 vote)
Get browser name and version, 9.0 out of 10 based on 1 rating
    Bluehost

    Mail this!

    To: From:Sum {3+8} =  
    Anything to add ?

        You must be logged in to post a comment.