Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Detect IE5 or IE6

function getMSIE6() {
       $userAgent = strtolower($_SERVER["HTTP_USER_AGENT"]);
       if (ereg("msie 6", $userAgent) || ereg("msie 5", $userAgent)) {
               return true;
       }
       return false;
}

Comments

  1. Peter Dubrovski
    Permalink to comment#

    Use preg_mach not ereg (depraced in PHP 5.3.0):

    function getMSIE6() {
    $userAgent = strtolower($_SERVER[“HTTP_USER_AGENT”]);
    if (preg_match(“/msie 6/”, $userAgent) || preg_match(“/msie 5/”, $userAgent)) {
    return true;
    }
    return false;
    }

  2. Adam Haskell
    Permalink to comment#

    How’s about simply

    function getMSIE56() {
    return preg_match(“/msie [56]{1}/i”, $_SERVER[‘HTTP_USER_AGENT’]);
    }

  3. Emmanuel
    Permalink to comment#

    Using strpos(“msie 6″, $userAgent) would be even better than eregi and preg_match

  4. Sarfraz Ahmed
    Permalink to comment#

    @Emmanuel: Using strpos with strict type comparison operator === or !== is the way to go due to strange behavior of it.

  5. Junior
    Permalink to comment#
    function getMSIE6() {
        return (preg_match('msie 6', $_SERVER["HTTP_USER_AGENT"]) || eregi('msie 5', $_SERVER["HTTP_USER_AGENT"]));
    }

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:

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