1

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,...)

rubo77
  • 15,234
  • 23
  • 111
  • 195
  • http://stackoverflow.com/questions/10020011/check-if-user-is-using-ie-with-compatibility-mode – daniel__ Jul 30 '13 at 08:45
  • 2
    that answer is good help, but it doesen't show how to use php to detect it – rubo77 Jul 30 '13 at 08:47
  • 1
    `Trident/6.0` means IE 10. I don't know how to get the compatibility mode from there, but I seriously doubt if you should develop for that. User agent strings are unreliable anyway, and your server side code should usually not be bothered at all with decisions like this. – GolezTrol Jul 30 '13 at 08:49
  • This might be interesting : http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx – Brewal Jul 30 '13 at 08:51
  • @MattWhipple That's about Javascript detection and is unrelated to the user agent string. – GolezTrol Jul 30 '13 at 08:53
  • what about searching for `MSIE 7.0;` and `Trident/6.0` at the same time? (by STT LCU) – rubo77 Jul 30 '13 at 08:53
  • 1
    If you're coming here for help, you should also be prepared to accept that what you want to do is not a good approach to whatever problem you're trying to solve. @GolezTrol's comment *is* on-topic. – user247702 Jul 30 '13 at 08:54
  • you are right, I deleted my comment – rubo77 Jul 30 '13 at 08:59
  • I'd be interested to know why you think you need to detect it at all? There are very few good reasons for using compat mode; if you explain what the circumstance is that led you to asking this question in the first place, I'd be willing to bet there's a better solution which removes compat mode from the equation entirely. – Spudley Jul 30 '13 at 09:08
  • re your edit (I only just saw it): I disagree. I don't think it's useful for either of those cases, since there's no reason why your users should ever get compat mode, unless your code explicitly sets it (in which case *all* your IE users will have compat mode, so there's no real point detecting it), or they manually change the settings in dev tools (in which case it's their problem, not yours, if the site breaks). There are a couple of minor edge cases, but in the main you shouldn't need to worry about it. If you are, just set `X-UA-Compatible` to `IE=edge` and even the edge cases go away. – Spudley Jul 30 '13 at 09:18
  • It differs, if you are in a local network with the PHP application or not, so not all users are having the same setting , see: http://stackoverflow.com/questions/2518256/override-intranet-compatibility-mode-ie8 > By default IE8 forces intranet websites into compatibility mode – rubo77 Jul 31 '13 at 08:11

2 Answers2

5

From this resource: http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx

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.

STT LCU
  • 4,258
  • 4
  • 26
  • 47
  • Then you know it it IE 10, but you still don't know the compatibility mode. – GolezTrol Jul 30 '13 at 08:48
  • Trident is in all versions – rubo77 Jul 30 '13 at 08:49
  • this is the IE10 user agent without compatibility mode: `Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)` and this is the IE10 user agent with compatibility mode: `Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Trident/6.0)`. If you see MSIE7.0 and Trident/6.0 the compatibility mode is enabled. It's not difficult to understand, trol. – STT LCU Jul 30 '13 at 08:50
  • @rubo77: yes but IE9 has `Trident/5.0` not `Trident/6.0`, for example. IE 8 has `Trident/4.0`, IE 7 has no Trident in it's user agent. – STT LCU Jul 30 '13 at 08:52
  • Then maybe you should have added that in your answer. – GolezTrol Jul 30 '13 at 08:57
  • 1
    @GolezTrol thnak you for the heads up, i'll fix my answer. – STT LCU Jul 30 '13 at 09:00
  • so I will detect `$version` and then: `$trident6=preg_match('/Trident\/6\.0;/i', $_SERVER['HTTP_USER_AGENT']); if($version==7 and $trident6) $compatmode = true;` – rubo77 Jul 30 '13 at 10:48
  • This stopped working on IE 11, it does not change $_SERVER['HTTP_USER_AGENT'] on compatibility mode, only on javascript level ... what can we do now!? – Caio Vianna Lima Netto Oct 22 '14 at 22:21
  • read the link i've posted at the top. Seriously, it's all written there. – STT LCU Oct 23 '14 at 06:32
  • IE11 for the desktop on 64-bit Windows 8.1 Update with compatibility view enabled Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0; Touch) – STT LCU Oct 23 '14 at 06:35
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.

Spudley
  • 157,081
  • 38
  • 222
  • 293
  • That is not entirely true. There are many variations to UA strings, and it's a fact that IE8+ browsers add `Trident/X` to the UA string even when they are in IE7 compatibility mode, while IE7 itself did not add it. But I couldn't agree more to your statement that detecting compatibility mode is a bad idea in the first place. :) – GolezTrol Jul 30 '13 at 08:56