浏览器检测
检测用户浏览器的最佳/最简单/最准确的方法是什么?
易于扩展和实施是一个优点。
使用的技术越少越好。
该解决方案可以是服务器端、客户端或两者。 不过,结果最终应该到达服务器。
该解决方案可以与框架无关。
该解决方案仅用于报告目的。
What's the best / simplest / most accurate way to detect the browser of a user?
Ease of extendability and implementation is a plus.
The less technologies used, the better.
The solution can be server side, client side, or both. The results should eventually end up at the server, though.
The solution can be framework agnostic.
The solution will only be used for reporting purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我最初问这个问题是因为我希望能够记录人们用来访问我的网站的浏览器和操作系统。 是的,用户代理字符串不可信,是的,您不应该使用浏览器检测来确定在 JS 中运行哪些代码,但是,我希望拥有尽可能准确的统计数据。
我做了以下事情。
我使用 JavaScript 和 PHP 的组合来记录统计数据。 JavaScript 确定浏览器和操作系统(因为这可能是最准确的),PHP 记录它:
JavaScript 来自 Quirksmode,PHP 是不言而喻的。 我使用 MooTools JS 框架。
将以下内容添加到 BrowserDetect 脚本中:
这将确定用户浏览器的浏览器、浏览器版本和操作系统,并将其发送到
record_browser.php
脚本。record_browser.php
脚本只是添加信息,以及 PHPsession_id
和当前user_id
(如果存在)。MySQL 表:
PHP 代码:
I originally asked the question because I want to be able to record the browsers and operations systems people use to access my site. Yes, the user agent string can't be trusted, and yes, you shouldn't use browser detection to determine what code to run in JS, but, I'd like to have as accurate as possible statistics.
I did the following.
I'm using a combination of JavaScript and PHP to record the stats. JavaScript to determine what browser and OS (as this is probably the most accurate), and PHP to record it:
The JavaScript comes from Quirksmode, the PHP is rather self evident. I use the MooTools JS framework.
Add the following to the BrowserDetect script:
This determines the browser, browser version and OS of the user's browser, and sends it to the
record_browser.php
script. Therecord_browser.php
script just add's the information, along with the PHPsession_id
and the currentuser_id
, if present.MySQL Table:
PHP Code:
编辑:不推荐使用以下解决方案。 试试这个: http://whichbrowser.net/
这曾经对我有用,但现在看看代码,我不知道怎么办。 请使用上面的内容:-/
不要忘记您需要初始化它才能使用它,因此请将其放入您的代码中:
然后检查浏览器类型和版本,如下所示:(例如 Internet Explorer 8)
等。
希望如此这项工作对您来说就像对我们一样,但请记住,没有浏览器检测是万无一失的!
Edit: The solution below isn't recommended. Try this instead: http://whichbrowser.net/
This once worked for me, but looking at the code now, I have no idea how. Use the above instead :-/
Don't forget that you need to initialize it to use it, so put this in your code:
And then check for a browser type and version like so: (e.g. Internet Explorer 8)
etc.
Hope it does the job for you like it has for us, but remember that no browser detection is bullet proof!
通常,当浏览器发出请求时,它会向您发送一堆信息(时间、名称日期、用户代理...)。 您应该尝试查看客户端发送的标头,然后转到告诉您他们的浏览器的标头(我认为它是“User-Agent:”。
Generally, when a browser makes a request, it sends a bunch of information to you (time, name date, user-agent...). You should try to look at the headers the client sent and go to the one that tells you their browser (I think it's "User-Agent:".
对于 Internet Explorer 和样式表,您可以使用以下语法:
这适用于 IE 6 或更早版本。
您可以更改 IE 版本,并且还可以:
我不确定这是否适用于页面的其他部分,但在放置在
标记中时有效。 请参阅此示例了解更多信息
For internet explorer and Style sheets you can use the following syntax:
This applys to IE 6 or earlier.
You can change the IE version and also have:
Im not sure if this works with other parts of the page but works when placed within the
<head>
tag. See this example for more information使用 javascript 时:不要使用浏览器检测
编写针对浏览器展示的给定情况进行自我测试的代码,否则您只是为非常小的人群编写代码。 最好在需要时使用
"typeof foo == 'undefined'"
和浏览器特定的技巧。jQuery 在其代码库中都做到了这一点(如果您查看代码,您会发现它实现了不同浏览器技术的行为)
从长远来看它更好。
When using javascript: Don't use browser detection
Write code that tests itself for given cases exhibited by browsers, otherwise you'll simply be writing code for a very very small population. Its better to use
"typeof foo == 'undefined'"
and browser specific tricks where you need them.jQuery does this all over its codebase ( if you look at the code you'll see it implementing behaviours for different browser tecnologies )
Its better in the long run.
由于我刚刚将其发布在(现已删除的问题)中,并且它仍在我的粘贴缓冲区中,因此我将重新发布。
注意:这是一个服务器端 PHP 解决方案,
我目前使用以下代码。 这并不是一个令人筋疲力尽的解决方案,但实现更多浏览器应该很容易。 我不知道 user-agents.org (感谢 PConroy),“其中一天”我将循环浏览它,看看是否可以更新并添加到我的列表中。
Since I just posted this in a (now-deleted question) and it's still in my paste buffer, I'll just repost.
Note: this is a server-side PHP solution
I currently use the following code for this. It is not nearly an exhausting solution, but it should be easy to implement more browsers. I didn't know about user-agents.org (thanks PConroy), "one of these days" I'll loop through it and see if I can update and add to my list.
在服务器上,您几乎只能使用浏览器提供的 UserAgent 字符串(这充满了问题,请阅读 UserAgent 字符串的历史记录)。
在客户端(即在 Javascript 中),您有更多选择。 但最好的选择是实际上不必担心确定它是什么浏览器。 只需检查以确保您想要使用的任何功能确实存在。
例如,您可能想要使用 setCapture,只有 MSIE 提供:
我们只是在使用之前查看它是否支持某些内容,而不是弄清楚浏览器是什么,然后推断其功能 - 谁知道哪些浏览器将支持哪些内容将来,如果 Safari 决定支持 setCapture,您是否真的希望必须返回并更新脚本?
On the server you're pretty much limited to the UserAgent string the browser provides (which is fraught with problems, have a read about the UserAgent string's history).
On the client (ie in Javascript), you have more options. But the best option is to not actually worry about working out which browser it is. Simply check to make sure whatever feature you want to use actually exists.
For example, you might want to use setCapture, which only MSIE provides:
Rather than working out what the browser is, and then inferring its capabilities, we're simply seeing if it supports something before using it - who knows what browsers will support what in the future, do you really want to have to go back and update your scripts if Safari decides to support setCapture?
JQuery 浏览器插件 将为您在客户端完成此操作。
该插件使
$.browser
可用,如果服务器端确实需要它,您可以通过 AJAX 调用将其重新提交到服务器。然而,该插件的有效性取决于它所测试的浏览器。 上面列出的插件有一个在其测试中识别的浏览器列表,但总是存在风险一个新的浏览器将会悄悄出现(Google Chrome..),这将需要重写识别规则。 也就是说,这个插件似乎会定期更新。
The JQuery Browser Plugin will do it client-side for you.
The plug-in makes
$.browser
available, which you can re-submit to your server via an AJAX call, if you really need it server-side.The plug-in will only be as effective as the browsers it's been tested against, however. The plugin listed above has a list of browsers recognised in it's tests, but there's always the risk that a new browser will come sneaking out (Google Chrome..) that will require a re-write of the recognition rules. That said, this plug-in seems to be regularly updated.
这是我使用的C#代码,希望对您有所帮助。
This is the C # code I use, I hope will be helpful.
正如许多人所说,浏览器检测可能会出错......但是为了 Code Golf 的利益。
这是检测 IE 的一种非常快速的方法。
实际上它非常简洁,因为它可以识别 IE,而不会在 Opera 上出错。
如果您知道为什么这在 IE 中有效,那就加分了。 ;-)
As stated by many, browser detection can go very wrong... however in the interests of Code Golf.
This is a very fast way to detect IE.
Its pretty neat actually because it picks out IE without tripping up on Opera.
Bonus points if you know why this works in IE. ;-)
不要使用浏览器检测:
最好在客户端脚本中进行功能检测。 您希望只需要浏览器检测来解决特定浏览器和版本中的错误。
Don't use browser detection:
It's better to do feature detection in client-script. You hopefully only need browser-detection to work around a bug in a specific browser and version.
正如 Dan 所说:这取决于所使用的技术。
对于 PHP 服务器端浏览器检测,我推荐 Harald Hope 的浏览器检测:
http://techpatterns.com/downloads/php_browser_detection .php
根据 GPL 发布。
as Dan said: it depends on the used technology.
For PHP server side browser detection i recommend Harald Hope's Browser detection:
http://techpatterns.com/downloads/php_browser_detection.php
Published under GPL.