Using PHP to change CSS Stylesheets depending on Browser

AshiKitty

New Member
Hiya guys, I have a problem. I'm helping my bf manage his site. He has made all of his web pages .php instead of .html in order to call php headers and footers and use them for all pages. The problem is that it looks different in some of the browsers. To correct it, I found this .php script for changing stylesheets that looks simple, and it works in most cases except for ours. He has a .php main page, then the main page calls the php header and footer. Inside the header file, I am attempting call .css files using PHP, and here is the code.


<?php
//detect browser and send style sheet for that browser
if (strstr ($_SERVER[ 'HTTP_USER_AGENT'], "MSIE"))
{ echo "<link rel='stylesheet' href='ie.css'>"; }

else if (strstr ($_SERVER['HTTP_USER_AGENT'], "Netscape"))
{ echo "<link rel='stylesheet' href='net.css'>"; }

else if (strstr ($_SERVER['HTTP_USER_AGENT'], "Firefox"))
{ echo "<link rel='stylesheet' href='moz.css'>" ; }

else if (strstr ($_SERVER['HTTP_USER_AGENT'], "Opera"))
{ echo "<link rel='stylesheet' href='opera.css'>" ; }

else { echo "<link rel='stylesheet' href='other.css'>"; }
?>


The only problem is, this code does not like to be called using a php include... but if I pasted it within the main body it works, but I dont want to have to paste this code in every single body because it is messy. Any ideas?

P.S. : I researched other methods, such as Java Script(looks messy and complicated), Conditional Comments(works only in IE), and CSS hacking, and I do think this code is the easiest way. This is what I want to achieve ultimately:
I want to be able to edit the looks in Opera, IE, FF, Avant and Safari.
I dont really care about earlier versions of browsers that much
 
Last edited:

jnjc

New Member
Can you provide some more info:

a) What does your include looks like
b) An example of a file calling it

I think you should try and resolve your issues without a separate .css for each browser. I think further down the line you'll regret having to maintain multiply .css. If you post and example we can take a look.

At very least have one main .css and have only the bits that need to be different per browser in each of the other .css files.

You will also need to check not just on browser type but also version. Things render differently between IE6 and IE7 likewise FF2 and FF3.
 

conor

New Member
theres no point in supporting netscape anymore - they stoped development for it last year and there is no longer any support for netscape users. ie. no one uses it anymore

i agree with jnjc on having one stylesheet. For cross-browser compatibility I usually allways need to just have an extra stylesheet for ie6. Is it something in particular that your having dificulty with compatibility wise?
 

AshiKitty

New Member
My include looks like...

Can you provide some more info:

a) What does your include looks like
b) An example of a file calling it

I think you should try and resolve your issues without a separate .css for each browser. I think further down the line you'll regret having to maintain multiply .css. If you post and example we can take a look.

At very least have one main .css and have only the bits that need to be different per browser in each of the other .css files.

You will also need to check not just on browser type but also version. Things render differently between IE6 and IE7 likewise FF2 and FF3.

The code pasted above was the include file and I call it like this in the header:

<href="stylesheet.php" rel="stylesheet" type="text/css">
 

AshiKitty

New Member
The difficulty is...

theres no point in supporting netscape anymore - they stoped development for it last year and there is no longer any support for netscape users. ie. no one uses it anymore

i agree with jnjc on having one stylesheet. For cross-browser compatibility I usually allways need to just have an extra stylesheet for ie6. Is it something in particular that your having dificulty with compatibility wise?

Yeah, the tables rent aligning/displayng property in IE, Avant and Safari.. the page looks just fine in Firefox and Opera. We arent really even using CSS yet, I thought I could help solve the problems using CSS...
 

Geodun1

New Member
Yeah, the tables rent aligning/displayng property in IE, Avant and Safari.. the page looks just fine in Firefox and Opera. We arent really even using CSS yet, I thought I could help solve the problems using CSS...

Although this COULD solve your problem, it's impractical if you ever want to make a change on the site. If you do, you have to change EACH of the stylesheets and then you have to bug check all of them. It might be easier just to code the compatibility in one CSS file. That way updates/changes are a lot easier. There are many articles on creating compatibility and thousands of "hacks" to get the rest of your stuff working.
 

AshiKitty

New Member
Although this COULD solve your problem, it's impractical if you ever want to make a change on the site. If you do, you have to change EACH of the stylesheets and then you have to bug check all of them. It might be easier just to code the compatibility in one CSS file. That way updates/changes are a lot easier. There are many articles on creating compatibility and thousands of "hacks" to get the rest of your stuff working.

Okay, but will these hacks still work in the future?

EDIT: Hm.. I guess conditional comments can be helpful if they work for IE and Avant. Thanks for your help! Now all I need to figure out is how to help Safari..
 
Last edited:

conor

New Member
The only problem is, this code does not like to be called using a php include... but if I pasted it within the main body it works, but I dont want to have to paste this code in every single body because it is messy. Any ideas?

You said it does not like having a php include? Are you sure that you have PHP installed locally on your machine? That could be the problem. Try looking for WAMP (windows) or LAMP (linux)
 
Theres no point in supporting netscape anymore - they stoped development for it last year and there is no longer any support for netscape users. ie. no one uses it anymore
 

ibrahimofic

New Member
did you use a template page?..if yes i would have advice you to use
<?php include(' stylesheet') ?> and late apply conditional comment.
 
Top