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();
?>
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments