Picnic Website Code Tutorials

IE PHP Browser Sniffer Tutorial

View Demo

This is Amazing! Bye-bye IE Conditional Comments, and hello the IE PHP browser sniffer! This method enables one to write all your "specific IE Only CSS" within the confines of your main style sheet. There are some other good PHP browsers sniffers floating around the web, but if all they do is feed each IE version a different style sheet, then they're no better than plain old IE CC's (when targeting IE). I took what another forum member posted at Killersites >> I trimmed and altered some of it to allow for targeting of IE in our main style sheet >> and a forum member named Krillz helped me clean/trim it even more. The html in all other browsers is left untarnished, and only IE gets a class="ie(+ version number)" on the body. What your left with is a NEW super clean way to target IE CSS!

Note: Because the way PHP works (server side), you won't see any evidence of the php code in the source code unless your viewing in IE. In which case all you'll see is the corresponding class on the body tag. Also, this works perfect in every browser except in older versions of Opera (around Opera 8 and below). Apparently, Opera used to give off a IE6 signature. However, Opera users upgrade regularly, so I would pay that no mind.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>IE PHP Browser Sniffer</title>
<?php $browser = ''; for($i = 6; $i<11; $i++ ) { if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE $i.0")) $browser = 'class="ie'.$i.'"';} ?>
<style type="text/css">
.ie6 p {color:red;}
.ie7 p {color:green;}
.ie8 p {color:blue;}
.ie9 p {color:yellow;}
.ie10 p {color:magenta;}
</style>
</head>
<body <?php echo $browser; ?>>

<p>Some Text</p>

</body>
</html>
		

Sponsors

Top Donators

Friends of Mine