Skip to content
Mar 15 / kkrizka

How To Detect Internet Explorer Version With PHP

Awhile ago I needed to detect the Internet Explorer version of the visitor from inside of my PHP script. Being lazy I just tried to Google for some existing code, but to my surprise there was none available! Well, I think it’s time to rectify that problem by publishing the code I ended up creating. The ieversion() function looks at the HTTP_USER_AGENT header sent by the browser to determine if the visitor is using Internet Explorer. If not, then the script will return -1. If yes, then the script will extract the version number and return that.

 

function ieversion() {
  $match=preg_match('/MSIE ([0-9].[0-9])/',$_SERVER['HTTP_USER_AGENT'],$reg);
  if($match==0)
    return -1;
  else
    return floatval($reg[1]);
}

What are the uses for this? You can do what I do and display a message notifying the visitor that he is using an outdated browser and he should upgrade to Firefox by clicking the Google Referal ad below. That solves two problems: Helps to pay the bills and I don’t have to make sure my site renders properly on the ancient Internet Explorer 6 (I let IE7 users get by, it does a pretty good job). I will publish the simple code I use for this later on.

UPDATE (June 20, 2010): As of version 6.0, PHP will no longer contain the ereg function that my original code used. I’ve updated it to use the now recommended preg_match function. The original code is still available below.

function ieversion() {
  ereg('MSIE ([0-9].[0-9])',$_SERVER['HTTP_USER_AGENT'],$reg);
  if(!isset($reg[1])) {
    return -1;
  } else {
    return floatval($reg[1]);
  }
}

23 Comments

Leave a comment
  1. Jeena / Mar 26 2008

    You wrote Exporer instead of Explorer in the title 😉

  2. Cyberto / Aug 28 2008

    Exactly what I needed! Thank you!!! 😀

    Greetings from Switzerland

  3. thomas / May 21 2009

    thanks 🙂 works fine 🙂

  4. Zurdito / May 25 2009

    Thanks a lot dude! 😀

  5. Achmed / May 28 2009

    Thanks.

  6. Tamar / Sep 23 2009

    thanks, it helped me a lot!

  7. ryan / Feb 3 2010

    Saved me time thanks for sharing! I’m surprised there isn’t already a function for this.

  8. 2Nine / Feb 24 2010

    works like a charm. thank you!!! 🙂

  9. Wayne / Mar 12 2010

    Hi,

    I added something extra.

    function ieversion() {
    ereg(‘MSIE ([0-9].[0-9])’,$_SERVER[‘HTTP_USER_AGENT’],$reg);
    if(!isset($reg[1])) {
    return -1;
    } else {
    //return floatval($reg[1]);
    if(floatval($reg[1]) == ‘6’){ //checks for evil eye of ie6
    return floatval($reg[1]); //here you can add other custom code as well

    }else{
    return ‘Not ie6, maybe 7 or 8’;
    }
    }
    }

    and to call it:

    echo ieversion();

  10. paulus / Jun 14 2010

    Thanks,
    I’ve used your tips to save my site from the virus named IE

    Paulus

    http://www.pmar.eu
    http://www.cybertek.it

  11. Jon / Jun 29 2010

    Note – ereg is deprecated so if you use this I think you should use preg_match. I’m not great with regular expressions so I’m not going to convert it.

  12. chipowski / Sep 21 2010

    brilliant. exactly what i’ve been looking for. didn’t want to use java to do this.

  13. Andrew Ensley / Mar 22 2011

    Exactly what I was looking for. Thank you. You saved me at least an hour.

  14. vio / Apr 19 2011

    Thank you! Exactly, what i’ve been looking for though I now noticed IE9 still can’t render xhtml fine. Here a one-liner:
    function IEVersion() {
    return preg_match(‘/MSIE ([0-9].[0-9])/’, $_SERVER[‘HTTP_USER_AGENT’], $reg) ? floatval($reg[1]) : 0.0;
    }

  15. Luis Arizaga / Apr 26 2011

    Thanks a lot man!! This is really useful!! Greetings from Argentina!

  16. Weboide / Aug 23 2011

    You’ll probably want to use: /MSIE ([0-9]+.[0-9]+)/ to be able to detect versions greater than 9.

  17. Charles / Mar 17 2012

    Thanks a bunch. This works great!

  18. Kassiny / Jul 16 2012

    Works perfectly well. Greetings from Nigeria

Trackbacks and Pingbacks

  1. enPHP » Blog Archive » Eliminar decoraciones PNG, si se usa Internet Explorer 6
  2. DevBlog » Blog Archive » Eliminar decoraciones PNG, si se usa Internet Explorer 6
  3. Detect older versions of IE with PHP | Adam Rehal
Leave a comment
Cancel reply