【发布时间】:2015-07-10 11:20:34
【问题描述】:
我的用户代理在 Opera 浏览器中测试时显示:
Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.125 Safari/537.36 OPR/30.0.1835.88
现在,我有以下脚本来测试浏览器:
$browser_list = array(
array('search_string' => 'chrome', 'bname' => 'Google Chrome', 'matchname' => 'chrome', 'ub' => 'Chrome'),
array('search_string' => 'firefox', 'bname' => 'Mozilla Firefox', 'matchname' => 'firefox', 'ub' => 'Firefox'),
array('search_string' => 'safari', 'bname' => 'Apple Safari', 'matchname' => 'Safari', 'ub' => 'Safari'),
array('search_string' => 'netscape', 'bname' => 'Netscape', 'matchname' => 'Netscape', 'ub' => 'Netscape'),
array('search_string' => 'opera', 'bname' => 'Opera', 'matchname' => 'Opera', 'ub' => 'Opera'), // Must be before IE
array('search_string' => 'MSIE', 'bname' => 'Internet Explorer', 'matchname' => 'IE', 'ub' => 'MSIE'),
array('search_string' => 'Mozilla', 'bname' => 'Internet Explorer', 'matchname' => 'IE', 'ub' => 'MSIE'), // Hack for IE 11 as the LAST one so you know not match the above
);
// Next get the name of the useragent yes seperately and for good reason
foreach ($browser_list as $item) {
echo $item['search_string'];
if (stripos($userAgent, $item['search_string']) !== false) {
$bname = $item['bname'];
$matchname = $item['matchname'];
$ub = $item['ub'];
echo "breaking";
break;
}
}
但是,由于 UserAgent 包含所有浏览器名称,所以我遇到了问题。
另外,为什么 Opera 的 usergent 这么奇怪?
【问题讨论】:
-
为什么要把事情复杂化。 Here 是一个伟大而简单的库,可以完全满足您的需求。
-
@Andrew,抱歉不能使用任何库...这是我的客户对这个问题的限制
-
伙计,运气不好。无论哪种方式,您都可以深入研究它的源代码,看看他是如何处理它的。
-
"另外,为什么 Opera 的 usergent 这么奇怪?" ——因为浏览器嗅探很糟糕,人们用它做了很糟糕的事情,浏览器试图弥补和绕过这些糟糕的事情。不要进行浏览器嗅探。
-
你需要浏览器名称做什么?
标签: php opera user-agent