Tutorial - A cross browser PHP based CSS selector - easy, scalable, W3C compliant, defeat IE7 trick buster

November 13, 2006 posted in tutorial by steven


I spent several hours over the weekend trying to google an easy way to simply change CSS based on the browser type. So i sought to develop a PHP CSS selector (i.e. a quick solution to a “only load this CSS file for this browser” function). I searched and searched - and did not seem to find anything that made any logical sense to me. So i decided to try to load a separate CSS file dynamically based on the $_SERVER[’HTTP_USER_AGENT’]; string value.

I ran into several issues - including:
* not being able to properly escape the link HTML inside of a PHP string variable to get an echo $HTML_VAR is properly show up as html and not a string
* not being able to manually use PHP to change the header types of php files (this would be a solution that basically replaces CSS with PHP files - which is something i have seen done to solve this problem but would not work in my particular LAMP configuration. If its a CSS file it should stay a CSS file IMHO - bad design to try to get my server to read PHP as CSS file i finally determined)

What i eventually develped is an assertion on the HTTP_USER_AGENT value - then based on a regular expression test for the ‘gecko’ string in the USER_AGENT value that will dynamically require-include a PHP file with a constant containing the HTML CSS link code(this can be easily modified to be much more granular - in this tutorial i kust did the cursory check for “gecko” in the AGENT string (firefox or mozilla) or ELSE).

Based on the above assertion - the PHP code will then include a remote PHP file with the actual HTML
code inside of an ENDH block (as a result of the issue faced above)

So what this will do in pseudocode is:

  • Does the USER_AGENT super string contain “gecko”
  • If yes - require mozilla php config file
  • moz php file streams in the proper CSS link to CSSHTML constant value
  • the PHP target page echos the CSSHTML constant CSS link code into the htm page
  • NOTE: Add the assertion PHP code into the header file or your .htm file or php files. CSSHTML is my constant value that will hold the proper link HTML for the required CSS stylesheet

    
    <?php
    
    $agent=$_SERVER['HTTP_USER_AGENT'];
    if(ereg ("Gecko",$agent))
            {
             require('includes/moz.php');
            }
    else 
            {
             require('includes/ie.php');
            }
    
    
    echo CSSHTML;
    ?>
    
    

    NOTE: Now create your browser sepcific links php config files. You can make one of these for each CSS sheet you might need for each browser class - mozilla, ie, or otherwise - or even more fun just Cascade any browser specific changes you need from a master CSS file. I just replaced the entire page to make the example easy to understand

    
    <?php
    //moz.php - put the CSS for your HTML pages between ENDH tags
    $moz_html=<<<ENDH
    <link rel="stylesheet" href="css/moz-style.css" type="text/css" />
    ENDH;
    
    define('CSSHTML',$moz_html);
    ?>
    
    
    

    that’s it! This CSS cross-browser support solution is scalable, W3C compliant, and can work through IE7 or any other newer browser functions that try to handle any “hide the CSS link tricks” that you may have used in the past
    (Eg: this kind of bad design: @import “null?\”\{”; )
    As we are learning - all the browser trick methods in my opinion are going to break with the newest versions of the market leading browsers - use the selector in this tutorial to prevent that.
    Hope you find this useful!

    tags:Technorati CSS, CSS browser compatibility, PHP, tutorial.
    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • Furl
    • del.icio.us
    • Reddit
    • Technorati
    • Slashdot
    • StumbleUpon
    • YahooMyWeb

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • Furl
    • del.icio.us
    • Reddit
    • Technorati
    • Slashdot
    • StumbleUpon
    • YahooMyWeb


    1 Comment »

    1. One particular script that has proven handy for a more granular seperation of user agents is below:

      
      function _get_browser()
      {
        $browser = array ( //reversed array
         \\\"OPERA\\\",
         \\\"MSIE\\\",            // parent
         \\\"NETSCAPE\\\",
         \\\"FIREFOX\\\",
         \\\"SAFARI\\\",
         \\\"KONQUEROR\\\",
         \\\"MOZILLA\\\"        // parent
        );
      
        $info[browser] = \\\"OTHER\\\";
      
        foreach ($browser as $parent)
        {
         if ( ($s = strpos(strtoupper($_SERVER[\\\'HTTP_USER_AGENT\\\']), $parent)) !== FALSE )
         {
           $f = $s + strlen($parent);
           $version = substr($_SERVER[\\\'HTTP_USER_AGENT\\\'], $f, 6);
           $version = preg_replace(\\\'/[^0-9,.]/\\\',\\\'\\\',$version);
      
           $info[browser] = $parent;
           $info[version] = $version;
           break; // first match wins
         }
        }
      
        return $info;
      }
      
      $info = _get_browser();
      $browser = $info[browser];
      $version = $info[version];
      

      This code snippet can provide excellent flexibility in design presentation for a large variety of browser types and has proven relatively effective to adjust for those annoying caveats between browsers, whether it be completely different layouts or small tweaks.

      Comment by lylix — November 15, 2006 @ 5:58 am

    RSS feed for comments on this post. TrackBack URI

    Leave a comment

    • Categories:
      • Uncategorized
      • general
      • eBay
      • asia china seo
      • tutorial
      • online advertising
      • seo
      • click-fraud
      • LAMP
      • sitemaps
      • google
      • pc product recommendations
      • viral ad campaigns
      • stealth advertising
      • free worlwide phone calls

    • Archives:
      • September 2007
      • May 2007
      • April 2007
      • January 2007
      • December 2006
      • November 2006
      • October 2006
      • September 2006
      • August 2006
    • Meta:
      • Login
      • RSS
      • Comments RSS
      • Valid XHTML

    Contact Roccanet
    Copyright © Roccanet Inc. 2006 All Rights Reserved.
    • Home
    • Services
    • Blog
    • Login
    • Contact