从Opera浏览器测试时,我的Useragent读取:
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的紧急情况如此奇怪?
答案 0 :(得分:1)
Mozilla / 5.0(Windows NT 5.1)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 43.0.2357.125 Safari / 537.36 OPR / 30.0.1835.88
您必须首先在用户代理中搜索“OPR /”字符串。 这就是Opera与其他浏览器的不同之处。
如果找到此字符串,请停止搜索。其他,继续。所以它不会与其他浏览器混淆。
答案 1 :(得分:-1)
新的浏览器通常擅长跟上新标准,甚至Microsoft的“ Edge”浏览器也相当快。
因此,除Firefox之外,所有其他浏览器制造商在useragent字符串中都包含“ Chrome”和“ Safari”,以确保显示浏览器嗅探功能以降低其他浏览器功能的网站具有完整功能。请参阅this answer to a related question,其中有更多详细信息。
See this for a detailed walkthrough on browser detection
通常,您应避免浏览器检测,而应尽可能使用功能检测,但这必须在需要特殊功能的客户端完成。
特别是关于Opera标记,因此当他们使用自己的“ Presto”引擎时,它一直被标识为“ Opera”,直到12版,但是此后,他们开始使用基于“ Blink”的引擎,因此他们必须开始使用其他标签来区别于旧的Opera引擎。
实际上,仍然有大量用户仍在使用Opera 12,这主要不是因为HTML引擎,而是因为其他高级功能,例如内置的电子邮件客户端。
在我拥有的测试站点中,我使用此javascript代码来嗅探用户代理并提取浏览器和版本信息以显示在页面上。我将其包括在内以可视化实际上是多么复杂的用户代理嗅探。 此代码还支持您可能不知道的浏览器,如果它们遵循该模式以使其唯一的浏览器/版本成为列表中的最后一个(对于最新的浏览器来说,这似乎是常见的模式-除了Chrome)。
var browserName = navigator.appName;
var fullVersion = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var nameOffset,verOffset,ix;
// In Opera (Presto, up to version 12), the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset+6);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset+5);
}
// In MSIE 11, the string "MSIE" is not longer present in userAgent
// Check for the engine "Trident" instead
else if ((verOffset=nAgt.indexOf("Trident"))!=-1) {
browserName = "Microsoft Internet Explorer";
verOffset = nAgt.indexOf("rv:");
fullVersion = nAgt.substring(verOffset+3);
}
// In Safari, the true version is after "Safari" or after "Version"
// Nowdays, most browsers include "Safari", so you also need to make sure it does NOT contain "Chrom" (as they also include "Crome", but Safari don't.
else if ((verOffset=nAgt.indexOf("Safari"))!=-1 && nAgt.indexOf("Chrom")<0) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset+7);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
(verOffset=nAgt.lastIndexOf('/')) )
{
browserName = nAgt.substring(nameOffset,verOffset);
if (browserName==="Safari") {
// Safari is last when browser is Chrome.
// Safari is handled already, so this has to be Chrome!
verOffset=nAgt.indexOf("Chrome") + 6;
browserName = "Chrome";
}
fullVersion = nAgt.substring(verOffset+1);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
fullVersion=fullVersion.substring(0,ix);
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
fullVersion = ''+parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion,10);
}
var engine = null;
if (window.navigator.appName == "Microsoft Internet Explorer")
{
// This is an IE browser. What mode is the engine in?
if (document.documentMode) // IE8 or later
engine = document.documentMode;
else // IE 5-7
{
engine = 5; // Assume quirks mode unless proven otherwise
if (document.compatMode)
{
if (document.compatMode == "CSS1Compat")
engine = 7; // standards mode
}
// There is no test for IE6 standards mode because that mode
// was replaced by IE7 standards mode; there is no emulation.
}
// the engine variable now contains the document compatibility mode.
}
var browserDetailsContent = ''
+'<li><b>Browser name: </b>' + browserName+'</li>'
+'<li><b>Full version: </b>' + fullVersion+'</li>'
+'<li><b>Major version: </b>' + majorVersion+'</li>'
+'<li><b>navigator.appVersion: </b>' + navigator.appVersion+'</li>'
+'<li><b>navigator.appMinorVersion: </b>' + navigator.appMinorVersion+'</li>'
+'<li><b>navigator.appCodeName: </b>' + navigator.appCodeName+'</li>'
+'<li><b>navigator.appName: </b>' + navigator.appName+'</li>'
+'<li><b>navigator.userAgent: </b>' + navigator.userAgent+'</li>'
+'<li><b>Internet Explorer engine: </b>' + engine+'</li>'
;
document.write(browserDetailsContent);