Simple Browser detection

/* browser detection code defenition*/
function browser_info($agent=null) {
$known = array(‘msie’, ‘firefox’, ‘safari’, ‘webkit’, ‘opera’, ‘netscape’, ‘konqueror’, ‘gecko’);
$agent = strtolower($agent ? $agent : $_SERVER[‘HTTP_USER_AGENT’]);
$pattern = ‘#(?<browser>’ . join(‘|’, $known) .
‘)[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#’;

if (!preg_match_all($pattern, $agent, $matches)) return array();
$i = count($matches[‘browser’])-1;
return array($matches[‘browser’][$i] => $matches[‘version’][$i]);
}
/* browser detection code defenition*/

 

Function call

$ua = browser_info();
foreach ($ua as $ind=>$vars){

if ($ind==’firefox’){
$ind=’Mozilla Firefox’;
}
else if($ind==’msie’){
$ind=’MS Internet Explorer’;
}
else if($ind==’safari’){
$ind=’Google Chrome/Safari’;
}

}

$browser=$ind.” “.$vars;

out put:

Mozilla Firefox 10.0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s