Using PHP to change CSS Stylesheets depending on Browser

Discussion in 'Programming' started by AshiKitty, Nov 16, 2008.

to remove all ads.
  1. AshiKitty

    AshiKitty New Member

    Messages:
    5
    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: Nov 16, 2008
  2. jnjc

    jnjc New Member

    Messages:
    493
    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.
     
  3. conor

    conor New Member

    Messages:
    348
    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?
     
  4. AshiKitty

    AshiKitty New Member

    Messages:
    5
    My include looks like...

    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">
     
  5. Geodun1

    Geodun1 New Member

    Messages:
    145
    Include it using <?php include("stylesheet.php"); ?>. See if that fixes your problem.
     
  6. AshiKitty

    AshiKitty New Member

    Messages:
    5
    The difficulty is...

    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...
     
  7. Geodun1

    Geodun1 New Member

    Messages:
    145
    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.
     
  8. AshiKitty

    AshiKitty New Member

    Messages:
    5
    It didnt work this time either.
     
  9. AshiKitty

    AshiKitty New Member

    Messages:
    5
    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: Nov 16, 2008
  10. Geodun1

    Geodun1 New Member

    Messages:
    145
    Try a google query on Safari Table Fix.
     
  11. conor

    conor New Member

    Messages:
    348
    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)
     
  12. designsatlanta

    designsatlanta Banned

    Messages:
    14
    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
     
  13. ibrahimofic

    ibrahimofic New Member

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

Share This Page