48

Is there a way to use PHP to detect if the page is being loaded using IE6?

4
  • Any particular reason you want to do this?
    – staticsan
    Mar 23, 2009 at 0:55
  • 4
    haha lets kill ie6 bringdownie6.com
    – lock
    Mar 23, 2009 at 3:33
  • 19
    Because it is old and time consuming to develop for and sometimes you want to just redirect them to a crappy, old version of your site. Oct 1, 2009 at 17:04
  • I want to add a message to my site for IE6 users telling them to update their junk already.
    – ddonche
    Mar 22, 2015 at 9:37

17 Answers 17

64

Try checking their user agent for 'MSIE 6.'.

$using_ie6 = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE);

This is based on this user agent information.

4
  • 8
    This will often match Opera as well, because Opera often adds "MSIE 6.0" within their user-agent string. Mar 23, 2009 at 3:35
  • 3
    Recent versions of Opera do NOT (by default) have MSIE 6.0 in the user-agent string
    – philfreo
    Dec 1, 2010 at 1:52
  • 2
    In my case, this is a false positive for all MSIE browser since all of them list "MSIE 6.0 compatible" even if their version is actually higher.
    – Christian
    Sep 29, 2011 at 14:04
  • 2
    @thomasrutter - Just add && !strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') to your if condition
    – Tom
    Aug 30, 2012 at 13:37
28

You can detect IE6 with HTML this way

<!--[if IE 6]>
// ie6 only stuff here
<![endif]-->

Here's a link on how it's done in PHPWay Back Machine but I've seen many false positives in parsing the $_SERVER["HTTP_USER_AGENT"] for IE6

1
  • 2
    This may be useful sometimes, but it isn't actually an answer to the question. It asks how to do so with PHP. The user agent might be IE, but it might not be rendering HTML. Jan 26, 2012 at 19:56
15

Many of the user-agent based answers on this page aren't too reliable because Opera often identifies itself with a user-agent string containing "MSIE 6.0", such as:

Mozilla/4.0 (compatible; MSIE 6.0; X11; Linux i686; en) Opera 9.51

This affects all versions of Opera 5 through 9 and even Opera 10 and can be turned on or off from within Opera. See this page.

A common approach I've seen is to test for "MSIE" and against "Opera". For example,

if (preg_match('/\bmsie 6/i', $ua) && !preg_match('/\bopera/i', $ua))
  echo "We have IE6!";
2
  • 2
    $ua = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/\bmsie [56]\./i', $ua) && !preg_match('/\b(opera|msie [789])/i', $ua)) { echo 'IE 5 or 6'; }
    – andrem
    Oct 22, 2010 at 1:11
  • 3
    strpos() will do the job quicker here.
    – Dunhamzzz
    Mar 2, 2011 at 14:23
12

well PHLAK...

i think this one is much better :P

  if(preg_match('/msie [2-6]/i',$_SERVER['HTTP_USER_AGENT'])) {
   // if IE<=6
  } else {
   //if IE>6
  }
6
  • Works great, except "preg" should be "preg_match". Nov 30, 2010 at 19:11
  • The syntax '/msie [1-6]/i' is shorter, and is probably more familiar to most people. Apr 28, 2012 at 17:18
  • yep, you right :) but it was 3 years ago and nowadays my regexp skills are more more much better than then ;)
    – marverix
    May 9, 2012 at 7:08
  • 1
    Note that this method will incorrectly identify IE10 as a match. Jan 25, 2013 at 2:59
  • the //if IE>6 comment is misleading. EVERY other browsers will fail the match.
    – designosis
    Jul 11, 2013 at 4:19
9

You can use get_browser with updated browscap.ini file.

1
  • This sounds interesting, I'll give it a try.
    – PHLAK
    Mar 23, 2009 at 0:57
6

Like everyone else said, there will be false positives by just checking the user agent... so why not use both, user agent checking and a conditional comment.

for example...

<? if (preg_match('/\bmsie 6/i', $_SERVER['HTTP_USER_AGENT']) { ?>
  <!--[if IE 6]>
    // ie6 only stuff here
  <![endif]-->
<? } ?>

This way you won't be sending back this unnecessary code to most browsers... but will still be safe in case of a false positive.

3

You can, using the HTTP User-Agent header, but I'd strongly advise not doing that if possible. The User-Agent header is very very difficult to parse accurately, and tends towards false positives with simple string matching — even ignoring the issue of browsers that pretend to be other browsers. For example Jeremy's “MSIE 6.” string will match IE Mobile, which is so very different from IE6 that you generally don't want to conflate them.

Plus when you send different HTML to different browsers, you have to use the ‘Vary’ header (which makes caches less effective) to avoid that caches send the wrong pages to different browsers.

So if you can find another place to do the browser differentiation that's definitely best. Ólafur's approach with conditional comments is usually the simplest approach for changing JavaScript and HTML markup/CSS links.

2

You can use it for many browsers, but it is time consuming and sometimes false positive...

function do_stylesheet_link() {
// Picks up appropriate css file depending on the user-agent

//  if ( stristr($_SERVER['HTTP_USER_AGENT'], 'Firefox') ) {
//echo '<link rel="stylesheet" href="css/firefox.css" type="text/css" />'; }    

//elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'Chrome') ) {
//echo '<link rel="stylesheet" href="css/chrome.css" type="text/css" />'; }

//elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'Safari') ) {
//echo '<link rel="stylesheet" href="css/safari.css" type="text/css" />'; }

//elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'Opera') ) {
//echo '<link rel="stylesheet" href="css/opera.css" type="text/css" />'; }

    if ( stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') )  {
echo '<link rel="stylesheet" href="css/ie6.css" type="text/css" />'; }

elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.') ) {
echo '<link rel="stylesheet" href="css/ie7.css" type="text/css" />'; }

elseif ( stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.') ) {
echo '<link rel="stylesheet" href="css/ie8.css" type="text/css" />'; }

else {
echo '<link rel="stylesheet" href="css/style.css" type="text/css" />';
}

}

0
2

if(substr($_SERVER['HTTP_USER_AGENT'],0,34)=="Mozilla/4.0 (compatible; MSIE 6.0;") echo "I am stupid IE6";

IE 6 Always starts with "Mozilla/4.0 (compatible; MSIE 6.0;" IE 7,8 have "MSIE 6" in the middle...

1

Thanks guys, I ended up creating the following function and calling it as needed:

// IE6 Check
function isIE() {
  $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  if (ereg("msie 6.0", $userAgent)) {
    return true;
  } else {
    return false;
  }
}
2
  • Quick tip: use the preg functions in PHP instead of ereg, they're faster. uk.php.net/manual/en/function.preg-match.php Mar 24, 2009 at 0:57
  • 4
    Scratch that, you shouldn't be using a regular expression function at all if you're only checking for the existence of a string. Use strpos as the answer above suggests. Mar 24, 2009 at 0:59
1

The methods listed will often flag browsers with certain plug-ins (MathPlayer, for instance; as well as some malware toolbars).

I find a much more reliable method is:

if( preg_match('/^Mozilla\/4\.0 \(compatible; MSIE 6/', $_SERVER['HTTP_USER_AGENT']) != 0 )
1
  • The snippet in the if returns TRUE if IE6 is detected.
    – AJ-D
    Jun 25, 2010 at 16:09
1

Something simpler:

if(stristr(strtolower($_SERVER['HTTP_USER_AGENT']), "msie 6.0")) {
    // IE6? Aren't you out of fashion yet?
}
1
function isIE($versions=array()) {
    if (!empty($versions))
        $versions = '('.implode('|',$versions).')';
    else
        $versions = '1?\d';
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $is_not_opera = false===stripos($ua, 'opera');
    return $is_not_opera && preg_match('/\bmsie '.$versions.'\./i', $ua);
}

Using:

$is_ie = isIE(); //Any version
$is_ie_7 = isIE(array(7)); //IE 7
$is_old_id = isIE(array(6,7));//IE 7 and 8
0
function isOldIE() {
    // don't support IE6 or below
    return preg_match('/MSIE [1-6]/', $_SERVER['HTTP_USER_AGENT']);
}
0

very helpful thread. I used this to hide the <?xml> declaration for IE6. Turns out IE6 only checks the first line of the document for doctype sniffing, which means that if the <?xml> string is present, IE6 will render in quirks mode regardless of the doctype following the <?xml> declaration

I'm not sure if its critical to include an opera check yet, but have included it anyway.

//check if IE 6 or less
$not_lte_ie6 = true;
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(strpos($ua,'msie') !== FALSE) {
    if(strpos($ua,'opera') === FALSE) {
        if(preg_match('/(?i)msie [1-6]/',$ua)) $not_lte_ie6 = false;
    }
}
if ($not_lte_ie6)
    echo "<?xml version='1.0' encoding='UTF-8'?>\n";
0
-1

Function is preg_match() instead of preg(), but good !

-1

Note IE8 also specifies that it is IE6 compatible in its user-agent string.

2
  • I found this out to be true recently when a user contacted me to say they were using IE8 but I detected it as IE6. Their user agent string was this: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)
    – neave
    Aug 1, 2011 at 0:58
  • I studied this and it seems that it only happens if the IE is in compatibility view.
    – Jonathan
    Jan 29, 2014 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.