I have a script that works fine in all browsers except ie6 (including ie7 and ie8).
Is there any semi reliable way I can exclude this nefarious browser.
I’ve tried this:
<!--[if !IE 6]>
<?php include("fconditionals.php"); ?>
<![endif]-->
and this:
$user_agent = getenv("HTTP_USER_AGENT");
if (preg_match("MSIE 6", $user_agent))
{
include("fconditionals.php");
}
The first thing you tried (Conditional Comments) won’t work because the include is done on the server, while the page is being generated, and the conditional comments are only checked by the browser after the page has been downloaded.
The conditional comments will exclude the generated code from being displayed in IE6, but it will still be run by the server.
Additionally, the conditional comments in the format you’ve used them here will also exclude the code from being displayed by all non-IE browsers. If you only want to affect IE, you need to re-format it so that the other browsers don’t treat the code inside as a comment, by using the
<![if !IE 6]>syntax instead of<!--[if !IE 6]>.That method still won’t stop the code from being run on the server though.
The second method you tried is more likely to be closer to what you actually want to do. However, please note that although PHP does receive the
USER_AGENTstring, it is possible in most browsers to spoof the USER_AGENT, and some privacy/security products actively remove it, as do some web proxies. In other words, the USER_AGENT string is not a 100% reliable way of determining what browser someone is using.That said, if you have specific PHP code that you want to exlude from being executed only for IE6, then it may be the only viable solution.
The reason this didn’t work for you is that your code includes only IE6 rather than excluding it. You need to add a ‘not’ operator (
!) in front of thepreg_match().By the way: In your example code, you have
$user_agent = getenv("HTTP_USER_AGENT");. It’s worth pointing out that$_SERVER['USER_AGENT']is already available as a variable, you don’t need to use getenv().