Forum Moderators: phranque

Message Too Old, No Replies

CSS with PHP

         

Sarah Atkinson

4:07 pm on Sep 2, 2005 (gmt 0)

10+ Year Member



I am using an IIS server and I want the server to parse my CSS for PHP (basicly so it will randomly change parts of my stylesheet) how can I do this?

encyclo

5:27 pm on Sep 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just name the file with a .php extension - you don't need to set it as .css - but at the very top of the file (before any content or even any white space) add a header which defines the MIME type of the file as
text/css
. I do this regularly and it works perfectly.

You will need to add a caching mechanism, otherwise your stylesheet will be called with each page view. I usually consider one hour to be enough, but you can adjust that. Here's the code:

<?php
ob_start ("ob_gzhandler"); // only needed if you want to gzip the file
[b]header("Content-Type: text/css");[/b]
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ; // 1 hour
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>

The header line is the most important thing. :)

For random changes, you should really be adding the styles directly into the head of the document - there is no advantage to using a separate stylesheet (in fact there is the disadvantage of having to call a separate file).