无法将Opera作为浏览器

时间:2014-01-05 18:31:20

标签: php

我有这个代码位来获取用户使用的浏览器

$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;

但我无法获得Opera,它将Opera视为Chrome。

Opera的代理是“Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 31.0.1650.63 Safari / 537.36 OPR / 18.0.1284.68”

如何才能让Opera成为Opera而不是Chrome?

2 个答案:

答案 0 :(得分:1)

您的数组声明中有两个Opera个键:

'Opera' => 'OPR',
'Opera' => 'Opera',

第二个是覆盖第一个,所以有效的数组看起来像:

$browserArray = array(
    'Windows Mobile' => 'IEMobile',
    'Android Mobile' => 'Android',
    'iPhone Mobile' => 'iPhone',
    'Firefox' => 'Firefox',
    'Opera' => 'Opera',
    'Google Chrome' => 'Chrome',
    'Internet Explorer' => 'MSIE',
    'Safari' => 'Safari'
);  

因此,您缺少OPR标识符,Chrome就是您的代理商。

答案 1 :(得分:0)

更简单的方法:

print_r(get_browser());

输出类似的内容:

    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] =>
)