2013-07-30 08:43
浏览 146


Is it possible to detect that IE is in compatibility mode from the useragent with PHP?

I use IE10 and have the useragent

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)

So it appears as Internet Explorer 7 then.

Sure, It would be a bad idea to rely on only such a detection by PHP, but it is very useful for some ocasions (for example logging with PHP or debugging-hints,...)

图片转代码服务由CSDN问答提供 功能建议

是否可以从使用者与PHP 检测到IE处于兼容模式?


  Mozilla / 4.0(兼容; 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)
   \  n 

因此它显示为Internet Explorer 7。

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba5540 2013-07-30 08:47

    From this resource:

    To detect IE 10 in compatibility mode rather than a regular IE 7 you should look at the token Trident/6.0 which identifies IE 10 regardless of the mode.

    To detect it from PHP, grab the user agent from the headers and parse it for the Trident/6.0 string token.

    You can recognize more versions of Internet Explorer from the Trident token: IE9 has Trident/5.0, IE 8 has Trident/4.0, IE 7 has no Trident in it's user agent.

    The user agent string can be found at $_SERVER['HTTP_USER_AGENT']. From there it's trivial as to search a substring inside or with a regex.

    IE10 User agent reference:

    • normal mode: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
    • compatibility mode: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0)

    Note that the MSIE token is different but the Trident token is the same. This is the indication that the user has compatibility mode enabled.

    打赏 评论
  • douchuang4402 2013-07-30 08:52

    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.

    打赏 评论

相关推荐 更多相似问题