note 58041 deleted from function.get-browser by bjori
| From: | bjori@php.net | Date: | Mon, 09 Oct 2006 10:59:33 +0000 |
| Subject: | note 58041 deleted from function.get-browser by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118255@lists.php.net to get a copy of this message | ||
Note Submitter: fotos at uop dot gr
----
Regarding the code by steve (in Feb) and somewhat corrected by matt there is another ugly bug laying
around:
Opera when set to report as IE returns the following header:
"Mozilla/4.0 (compatible; MSIE 6.0; Mac_PowerPC Mac OS X; en) Opera 8.5"
Well in the code given the last matching $browser in the array wins, and in this case the code
reports "Mozilla" which is totally unexpected!
But Safari gives:
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412.7 (KHTML, like Gecko)
Safari/412.5"
The solution I can think of is to first search for more specific browsers and then for more generic
(also seems more reasonable). If something matches then we are done. But probably this is not a
catch all solution, but a good starting point.
So the code becomes:
<?php
function _get_browser()
{
$browser = array ( //reversed array
"OPERA",
"MSIE", // parent
"NETSCAPE",
"FIREFOX",
"SAFARI",
"KONQUEROR",
"MOZILLA" // parent
);
$info[browser] = "OTHER";
foreach ($browser as $parent)
{
if ( ($s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent)) !== FALSE )
{
$f = $s + strlen($parent);
$version = substr($_SERVER['HTTP_USER_AGENT'], $f, 5);
$version = preg_replace('/[^0-9,.]/','',$version);
$info[browser] = $parent;
$info[version] = $version;
break; // first match wins
}
}
return $info;
}
?>