note 58041 added to function.get-browser
| From: | fotos at uop dot gr | Date: | Sat, 22 Oct 2005 02:35:44 +0000 |
| Subject: | note 58041 added to function.get-browser | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97399@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.get-browser.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58041
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58041&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58041&report=yes
Search -- http://master.php.net/manage/user-notes.php