Grow your CSS skills. Land your dream job.

Last updated on:

PHP IE Detection

if ( eregi("MSIE", getenv( "HTTP_USER_AGENT" ) ) || eregi("Internet Explorer", getenv("HTTP_USER_AGENT" ) ) ) {
  // do something
}

Comments

  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

    • 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. 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. 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_browser++;
    }    
     
    $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
    $mobile_agents = array(
        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
        'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
        'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
        'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
        'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
        'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
        'wapr','webc','winw','winw','xda ','xda-');
     
    if (in_array($mobile_ua,$mobile_agents)) {
        $mobile_browser++;
    }
     
    if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
        $mobile_browser++;
    }
     
    if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
        $mobile_browser = 0;
    }
     
    if ($mobile_browser > 0) {
       header("Location: http://tkpb.tk/m/");
    }
    else {
       header("Location: http://tkpb.tk/home.html");
    }   
    ?>
    
  6. Permalink to comment#

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

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

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

```
<div>Example code</div>
```

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".