21361

Detecting IE browser version using PHP

Question:

I'm using $_SERVER['HTTP_USER_AGENT'] to detect user's browser. When I run the var_dump on my localhost on IE8 it returns: ...compatible; MSIE 8.0; ... But when I upload it to my host (godaddy), I get: ...compatible; MSIE 7.0; ...

What's the problem?

Answer1:

Check for the word TRIDENT in the string. The compatibility-thing is ie in compatibility-mode. Google it if you want to know what it means. However, the term trident is only ie8+.

Edit: Also, you can set a headder/meta-tag to force ie8-mode (no compatibility). However, this will probably not take effect before the second request. Google that too.

Answer2:

<blockquote>

I'm using $_SERVER['HTTP_USER_AGENT'] to detect user's browser

</blockquote>

Yeah, that's the problem. Don't do that.

User-Agent string-hacking is a losing game. There are any number of odd cases that are likely to confuse your scripts.

This is one of them: IE8, when defaulting to Compatibility Mode, pretends to be IE7. You can detect this case by the presence of Trident/... in the string, but of course like all string-hacking ‘solutions’ this'll go wrong if that string happens to be present for other reasons, which it might be given that any application can stick arbitrary strings to the end of IE's User-Agent header, and indeed given that browsers regularly lie about who they are, and intermediaries may change or remove the header.

Also by doing browser-sniffing at the server-side, you are making your HTTP responses depend on the browser viewing them, which means that proxy caches will serve the wrong page to the wrong browser unless you include the proper Vary header. But if you do that, you break caching in IE.

Server-side UA sniffing is a horror that you should only ever use as a last resort if it really isn't possible to use any other technique. For the usual case of showing different content to particular versions of IE, you are much better off with conditional comments.

Answer3:

Try <a href="http://php.net/manual/en/function.get-browser.php" rel="nofollow">get_browser()</a> and see what happens.

From the link:

<?php echo $_SERVER['HTTP_USER_AGENT'] . "\n\n"; $browser = get_browser(null, true); print_r($browser); ?>

Output:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3 Array ( [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$ [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* [parent] => Firefox 0.9 [platform] => WinXP [browser] => Firefox // ...

Answer4:

preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches); echo $version = $matches[1];

Recommend

  • How to define “M-x find” in the same way as “M-x grep”?
  • Using variable in sql postdeployment build script?
  • Converting Julian date to Java date but still cannot get Month
  • Thrift gem and JRuby
  • choosing right approch to implement google map in android
  • Get some trounble when using drawBuffers in WebGL2
  • Emma reports 0% coverage
  • Why can't I use the keyword “this” when referring to fields in static methods?
  • How to fix this floating point square root algorithm
  • white-space: pre-wrap not working in IE
  • Is it expensive to create the Thread object or to actually start the thread?
  • Python 3 Sorting a List of Tuples?
  • Different builds of turn-based Game Center game can’t see each other’s matches
  • Perl keyword say is not working in version 5.14.4
  • opengl window freezing during move/resize
  • Enable Bootstrap Intellisense using Angular 4 in WebStorm
  • FragmentActivity with a Fragment Containing a MapView
  • crash in __tcf_0
  • python: forcing relative imports to search from script file
  • Python 3.2.2, error(scripts to exe)
  • Request response issues in biztalk
  • BeautifulSoup difference between findAll and findChildren
  • For loop with if condition on multiple R functions
  • Position: fixed nav does not stay fixed
  • How can I speed up CURL tasks?
  • Content-Length header not returned from Pylons response
  • Test if a set exists before trying to drop it
  • Bad request using file_get_contents for PUT request in PHP
  • Seeking advice on Jetty HttpClient Hang
  • Is there a javascript serializer for JSON.Net?
  • How would I use PHP exceptions to define a redirect?
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Where to put my custom functions in Wordpress?
  • Symfony2: How to get request parameter
  • PHP: When would you need the self:: keyword?
  • Buffer size for converting unsigned long to string
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • Append folder name and increment by 1 using batch script
  • Binding checkboxes to object values in AngularJs