Is there a way to use PHP to detect if the page is being loaded using IE6?
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 PHP but ive seen many false positives in parsing the $_SERVER["HTTP_USER_AGENT"]
for IE6
Try checking their user agent for 'MSIE 6.'
.
$using_ie6 = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE);
(This is based on the user agent information found here: http://www.useragentstring.com/pages/Internet%20Explorer/.)
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.
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;
}
}
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!";
Note IE8 also specifies that it is IE6 compatible in its user-agent string.
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";
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 )
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.