Get a free trial // Grow your CSS skills // Land your dream job

PHP IE Detection

Last updated on:
if ( eregi("MSIE", getenv( "HTTP_USER_AGENT" ) ) || eregi("Internet Explorer", getenv("HTTP_USER_AGENT" ) ) ) {
  // do something


  1. Guy
    Permalink to comment#

    Have a quick maybe stupid question. I need to detect all IE versions lower then 8. Can this just be done using the checks for if it’s ie 6 / 7 or is there an easier way

    • jayclimbs
      Permalink to comment#

      Browsers send their ID in a header file; I’m not sure if IE 8 sends anything unique from previous versions, but if you look at the header of it compared to other versions, you may find a useable text-string.
      Make a script that echoes HTTP_USER_AGENT, find some folks who have earlier versions, have them send you the result, and see if any text string fits the bill.
      Keep in mind, you may also want to differentiate between IEPC and IEMac.
      I once used a switch statement with several browser options such as this snippet, and fed different CSS stylesheets to the output, depending on which browser was requesting a page. I didn’t parse between versions, however.

  2. Adam Haskell
    Permalink to comment#

    Important note: Some non-ie browsers include the substring MSIE in them. Namely Opera.

  3. Geert
    Permalink to comment#

    Note that the POSIX regex functions, such as eregi, are deprecated since PHP 5.3.0. Here’s a version using preg_match:

    if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT'])) { }
  4. Josh
    Permalink to comment#

    Does this work for Windows mobile 7 too? Thanks

  5. Kalleb
    Permalink to comment#

    It doesn’t works for me, even the PHP5 version (from Geert). :(
    I use this for detecting mobile browsers, and I wanted the same php file detects if its IE, and then redirects IE users to a directory on my site.
    Could someone help me? I don’t know much PHP. :\

     0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobile_agents = array(
        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
        'wapr','webc','winw','winw','xda ','xda-');
    if (in_array($mobile_ua,$mobile_agents)) {
    if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
        $mobile_browser = 0;
    if ($mobile_browser > 0) {
    else {
  6. Kalleb
    Permalink to comment#

    Oh, please forget my earlier comment. I got it working.
    Thanks CSS-Tricks and Geert!

  7. Kevin
    Permalink to comment#

    +1 to Geert! Saved me hours of looking! Thanks

  8. Nathan
    Permalink to comment#

    Kevin and Geert – what was the fix? I was going to try the code but it appears that the incorrect code is being shown, and the correction did not get explained to the audience. Any chance one of you could address it? I’m trying to sniff IE versions now, and I’m new to PHP! Thanks for any help you can offer!

  9. Thomas SERES

    In you want to include IE 11 in the condition :
    if (preg_match(‘~MSIE|Internet Explorer~i’, $_SERVER[‘HTTP_USER_AGENT’]) || (strpos($_SERVER[‘HTTP_USER_AGENT’], ‘Trident/7.0; rv:11.0’) !== false)) {
    // Do something

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed