Submit your breaking news stories and original articles to us by contacting us
The more I thought about it the more I began to see some of the benefits of generating style sheets with PHP. It can help keep related styles together in one document as opposed to having separate files for various browsers and JavaScript can be used on the client side to pass values back to the server so a custom style sheet can be created.
I know I have seen this discussed at other places and I believe the major problem was that the stylesheet doesn’t get cached by the user’s browser so it has to reload every single time they visit the site. Don’t know how much of a disadvantage that would be to you.
Category: Uncategorized
3 Responses for "Generating CSS with PHP"
June 24th, 2004 at 5:08 am
1True….sorta.
If it ends in .php, there is a good chance it will not cache it, but this can be avoided by forcing cache headers down the browsers throat.
In addition, if that’s not enough, you can use a good ‘ol .htaccess file to redirect “joe.css” to send “joe.css.php” and the client (browser) will be none the wiser.
This snippet at the front of a “joe.css.php” file will not only make it send the correct mime type and cache headers, but will also gzip the content (if the browser accepts it). Can reduce something like 1/5 the size. Nifty.
<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
June 24th, 2004 at 10:46 am
2Andy (Milbertus.com) and I were working on this for his site a while back… the bigger problem is that mozilla based browsers can’t handle it unless you jump through hoops.
You have to:
set .css to parse as .php
pass the text/css mimetype in a header()
ditto for javascript generated by PHP, IIRC
This is why I prefer using all my stylesheets embedded in the document instead of as a separate file… I design using templates or at minimum a global header and footer file anyway, so I’m still only modifying one file. It stops me from being able to do some of the stupid browser tricks, but I never do anything that complicated with CSS anyway.
June 24th, 2004 at 10:49 am
3as for caching and gzipping…
gzipping sometimes screws up CSS files, which is why commercial products to enable gzipping usually offer options to ignore css and js files (as well as specific other files)
caching… just stick an expires line in your htaccess file, and make sure you change the filename if you modify the stylesheet.
RSS feed for comments on this post
Leave a reply