Jo Sprague
Jo Sprague

Reputation: 17373

Testing for IE with PHP

I am developing a web page that will be used in several different contexts, and while I normally avoid browser sniffing and conditional statements at all costs, there is just no way around it this time.

I'm trying a simple method, and I was hoping I could get some feedback on whether this is a good idea or not.

I am testing for the user agent and then echoing a class on the element in the html (in my case, a div) if the user agent is IE.

<?php echo (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'msie') !== FALSE) ? ('classforie') : (''); ?>

Then in my css, I am doing something along the lines of this;

.classone { display: inline-block; }
.classone.classforie { zoom: 1; display: inline; }

Note, the CSS isn't the important part, I just made that up for the sake of example. I'm just wondering if doing things this way is a good practice or not?

Upvotes: 1

Views: 127

Answers (2)

Alexandru Petrescu
Alexandru Petrescu

Reputation: 3479

.classone.classforie { zoom: 1; display: inline; }

Definitely does not work in IE6.

The way I've usually seen it done is to output the class on the BODY tag. I think there might be some issues with IE6 and classes on the HTML tag.

Then you will have something like this:

.msie .classone { zoom: 1; display: inline; }

Upvotes: 1

Quentin
Quentin

Reputation: 943561

No. Use conditional comments. User agent sniffing is unreliable.

Upvotes: 5

Related Questions