Reputation: 541
I have this code bit to get which browser the user uses
$browserArray = array(
'Windows Mobile' => 'IEMobile',
'Android Mobile' => 'Android',
'iPhone Mobile' => 'iPhone',
'Firefox' => 'Firefox',
'Opera' => 'OPR',
'Google Chrome' => 'Chrome',
'Internet Explorer' => 'MSIE',
'Opera' => 'Opera',
'Safari' => 'Safari'
);
foreach ($browserArray as $k => $v) {
if (preg_match("/$v/", $agent)) {
break;
} else {
$k = "Unknown";
}
}
$browser = $k;
But I can't get Opera, it returns Opera as Chrome.
The agent for Opera is "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 OPR/18.0.1284.68"
How can I make it get that Opera is Opera and not Chrome?
Upvotes: 0
Views: 103
Reputation: 2269
Simpler way:
print_r(get_browser());
outputs something like that:
Array
(
[browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
[browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
[parent] => Firefox 0.9
[platform] => WinXP
[browser] => Firefox
[version] => 0.9
[majorver] => 0
[minorver] => 9
[cssversion] => 2
[frames] => 1
[iframes] => 1
[tables] => 1
[cookies] => 1
[backgroundsounds] =>
[vbscript] =>
[javascript] => 1
[javaapplets] => 1
[activexcontrols] =>
[cdf] =>
[aol] =>
[beta] => 1
[win16] =>
[crawler] =>
[stripper] =>
[wap] =>
[netclr] =>
)
Upvotes: 0
Reputation: 29482
You have two Opera
keys in your array declaration:
'Opera' => 'OPR',
'Opera' => 'Opera',
Second one is overwriting firs, so effectively you array looks like:
$browserArray = array(
'Windows Mobile' => 'IEMobile',
'Android Mobile' => 'Android',
'iPhone Mobile' => 'iPhone',
'Firefox' => 'Firefox',
'Opera' => 'Opera',
'Google Chrome' => 'Chrome',
'Internet Explorer' => 'MSIE',
'Safari' => 'Safari'
);
So you are missing OPR
identifier and Chrome is the one matching your agent.
Upvotes: 1