Web Hosting Talk







View Full Version : PHP script help, different CSS for different browsers...


darthvadersmaster
06-02-2006, 12:55 PM
When I was designing my layout for my site, I ran into some CSS positioning problems...the trouble was things were completely whacked in IE. So instead of wasting my time trying to figure out what the crap was wrong, I just decided I'll use different CSS for different browsers. So, whenever a user views my site in IE, special IE CSS is printed, but if they view it in any other browser, they get the CSS that works for them. But of course, another problem came up...at my school today, I went to my site to show off to some n00b HTML authors, and guess what? The site was as messed up as ever on IE. What on earth wrong? I'm using the latest version of IE on my computer (excluding IE 7 Beta...), and I'd assume the school is too, right? But then why does everything look messed up over there? Same browser, same resolution. Only difference is they use XP, I use 2000. Some help please :(

<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
if($browser == 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; PeoplePal 3.0)') {
include('/home/neobpal/public_html/stylesheets/main_ie.php');}
elseif($browser == 'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0; .NET CLR 1.1.4322; PeoplePal 3.0') {
include('/home/neobpal/public_html/stylesheets/main_ie.php');}
elseif ($browser == 'Mozilla/4.0 (compatible; MSIE 4.0; Windows NT 5.0; .NET CLR 1.1.4322; PeoplePal 3.0'){
include('/home/neobpal/public_html/stylesheets/main_ie.php');}
else {
include('/home/neobpal/public_html/stylesheets/main.php');
}
?>

fozzy
06-02-2006, 01:14 PM
Are you sure that it is outputting the sheet you made for IE? You have the user agents spelled out in long form, what if the version they use give a slightly differnt string for the user agent? You will get the 'normal' CSS file instead of the IE one.

I am not super familiar with all the different possiblities that can be reported for user agents but that was my first though.

arkin
06-02-2006, 02:44 PM
Your code is right in a sense but only affects your browser, you shouldn't be looking at OS, you should more focus on the Browser and pick out common parts. For all 3 of your if statements there is a MSIE located somewhere within the string, this means Microsoft Internet Explorer (duh? :)).

Anyway, A much simpler peice of code would be.

<?php
$css_file=(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))?'/home/neobpal/public_html/stylesheets/main_ie.php':'/home/neobpal/public_html/stylesheets/main.php';
include($css_file);
?>

The operation inside the include brackets is the same of an if statement, the strstr searches the user_agent for MSIE, if its found it will include main_ie.php otherwise just main.php. I hope this works for you.

darthvadersmaster
06-02-2006, 03:15 PM
Ah, thank you. I had just realized that it included the OS in there, and I was looking for a way around that...then I saw your post. Thanks :)