1

是否可以从用户代理与 PHP检测到 IE 处于兼容模式?

我使用 IE10 并拥有用户代理

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)

所以它显示为 Internet Explorer 7。

当然,仅依靠 PHP 的这种检测是一个坏主意,但它在某些情况下非常有用(例如使用 PHP 进行日志记录或调试提示,...)

4

2 回答 2

5

从这个资源: http: //msdn.microsoft.com/en-us/library/ie/hh869301 (v=vs.85).aspx

要在兼容模式而不是常规 IE 7 中检测 IE 10,您应该查看Trident/6.0标识 IE 10 的令牌(无论模式如何)。

要从 PHP 中检测它,请从标头中获取用户代理并将其解析为Trident/6.0字符串标记。

您可以通过 Trident 令牌识别更多版本的 Internet Explorer:IE9 有Trident/5.0IE 8 有Trident/4.0IE 7 的用户代理中没有 Trident。

用户代理字符串可以在 中找到$_SERVER['HTTP_USER_AGENT']。从那里搜索内部或使用正则表达式的子字符串是微不足道的。

IE10 用户代理参考:

  • 正常模式:Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
  • 兼容模式:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0)

请注意,MSIE 令牌不同,但 Trident 令牌相同。这表明用户启用了兼容模式。

于 2013-07-30T08:47:35.853 回答
2

No it isn't.

Firstly, the whole point of compat mode is that it is pretending to be whatever old version of IE has been specified. The UA string is changed by default as part of that, and it is identical to the UA string provided by a real copy of the old IE version that is being emulated.

Secondly the browser mode (ie rendering mode) and the document mode (ie the user agent) can be set separately in the dev tools; it is possible to be in IE8 compat mode but still show the IE10 user agent.

In short, trying to detect compat mode using the UA string is a bad idea; it's simply not possible, and even if it were possible, it could easily be spoofed.

However, you should never have a valid reason to actually need to do this. If your site is telling IE to display the page to compat mode, then the server should already know this; it shouldn't need to check. There are a few edge cases where it might come up unexpectedly, but you can work around those with the X-UA-Compatible meta tag.

Best practice is to avoid compat mode if at all possible anyway; if you've written your site properly, compat mode should be completely unnecessary. If you do find yourself needing it, you'd be better off fixing your code to work properly in IE10.

于 2013-07-30T08:52:36.987 回答