使用php进行可靠的用户浏览器检测 [英] reliable user browser detection with php

查看:104
本文介绍了使用php进行可靠的用户浏览器检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅尝试使用PHP检测用户的浏览器,$ _ SERVER ['HTTP_USER_AGENT']是一种可靠的方法吗?我是否应该选择 get_browser 函数?您发现哪一个带来了更精确的结果?

Trying to detect a user's browser with PHP only, is $_SERVER['HTTP_USER_AGENT'] a reliable way? Should I instead opt for the get_browser function? which one do you find brings more precise results?

如果此方法很实用,是否建议将其用于输出相关的CSS链接,例如:

If this method is pragmatic, is it ill advised to use it for outputting pertinent CSS links, for example:

if(stripos($_SERVER['HTTP_USER_AGENT'],"mozilla")!==false)
   echo '<link type="text/css" href="mozilla.css" />';

我注意到了这个问题,但是我想澄清一下这是否是对面向CSS的检测很有用.

I noticed this question, however I wanted to clarify whether this is good for CSS-oriented detection.

更新: 真的很可疑:我在IE 7上尝试了echo $_SERVER['HTTP_USER_AGENT'];,这就是它的输出:

UPDATE: something really suspicious: I tried echo $_SERVER['HTTP_USER_AGENT']; on IE 7 and this is what it output:

Mozilla/4.0(兼容; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618)

Safari也给它添加了"mozilla"的怪异之处.有什么作用?

Safari gave something weird with "mozilla" in it too. What gives?

推荐答案

使用现有方法(即 get_browser )可能比自己编写东西要好,因为它具有(更好的)支持,并且将使用较新的版本进行更新.可能还有可用的库,以可靠的方式获取浏览器ID.

Using an existing method (ie get_browser) is probably better than writing something yourself, since it has (better) support and will be updated with newer versions. There might be also usable libraries out there for getting the browser id in a reliable way.

$_SERVER['HTTP_USER_AGENT']进行解码非常困难,因为许多浏览器具有非常相似的数据,并且倾向于(滥用)它们以获取自己的利益.但是,如果您真的想对它们进行解码,则可以使用此页面上的信息所有可用的代理ID. 此页面还显示您的示例输出确实属于IE7.有关代理ID本身的字段的更多信息,可以在

Decoding the $_SERVER['HTTP_USER_AGENT'] is difficult, since a lot of browsers have quite similar data and tend to (mis)use it for their own benefits. But if you really want to decode them, you could use the information on this page for all available agent ids. This page also shows that your example output indeed belongs to IE 7. More information about the fields in the agent id itself can be found on this page, but as I said already browsers tend to use it for their own benefits and it could be in a (slightly) other format.

这篇关于使用php进行可靠的用户浏览器检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆