PHP IE Detection

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

Comments

  1. User Avatar
    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

    • User Avatar
      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. User Avatar
    Adam Haskell
    Permalink to comment#

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

  3. User Avatar
    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. User Avatar
    Josh
    Permalink to comment#

    Does this work for Windows mobile 7 too? Thanks

  5. User Avatar
    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_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. User Avatar
    Kalleb
    Permalink to comment#

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

  7. User Avatar
    Kevin
    Permalink to comment#

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

  8. User Avatar
    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. User Avatar
    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
    }

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag