Code Snippet

Home » Code Snippets » PHP » Detect IE5 or IE6

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;
}

Subscribe to The Thread

  1. Peter Dubrovski

    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. How’s about simply

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

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

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

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~