Start your CSS files with this PHP (and name it style.php):
<?php
ob_start ("ob_gzhandler");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$offset = 60 * 60 ;
$ExpStr = "Expires: " .
gmdate("D, d M Y H:i:s",
time() + $offset) . " GMT";
header($ExpStr);
?>
body { color: red; }
Then call your CSS with the PHP file name:
<link rel='stylesheet' type='text/css' href='css/style.php' />
Some older browsers choke when CSS files don’t have the .css extention. It’s the same with Javascript files.
Perhaps use .htaccess to rename it to style.css
Anyway nobody cares about old browsers when developing Web sites.
@Ben: Or rename the file to .css and use .htaccess to interpretate is as PHP ;-)
@Ben: Or rename the file to .css and use .htaccess to interpretate it as PHP ;-)
I believe it’s good practice to call ob_flush(); at the end of your script.
how do you know so much stuff
I’m glad to see my submission got up on the site.
I get “Cannot modify header information” error. Any idea?
You may be outputting something before the call to header().
Usually this is because of a new line or space before the very first <?php tag
Check for UTF8 Byte Order Marks in your file. That causes the issue here.
Thanks for the snippet. The line
<pre>header(“Content-type: text/css; charset: UTF-8”);</pre>
should be
<pre>header(“Content-type: text/css; charset=UTF-8”);</pre>
What are the advantages if i compress css?:)
Google speed test :P
i think you can also use gzip or defalte mod options if you are using apache server.
use the ob_gzhanlder() callback.
ob_start(‘ob_gzhandler’);
available since PHP 4.0.4
What is the uses ?
-thanks in advance.. :)
What are the advantages of it? I am using Minify stuff?
you could use it like this:
This script has disadvantages. The main disadvantage is caching.
On first visit, php will set expire date to “future”, but… if you make change into the CSS, will not reflect to returning visitors, because they already have cached CSS.
True, but there are other methods of caching, such as setting an ETag header and changing that whenever you change the contents of the php output so as to update the browser’s cache. Or better yet, do a checksum of the output buffer and set that as the ETag that way you never have to touch the php code, although that has disadvantages because you’d have to render the css and hash it each time to compare the ETag from the client.
http://en.wikipedia.org/wiki/HTTP_ETag
Quick note, I didn’t test the above code so it may or may not work.
Or you can use a very quick and common fix for caching, time() or something along those lines. I personally do style.php?r=md5 ( 2012-01-13_09-51pm )
md5 hashing revision date of css so if there is a new md5 hash it will re-cache the css, since time() will force non-stop reaching which is not something you may wont.
But any way good and very important specially when google bot visit its also check how quick is your website. I dont think this is a disadvantage. Its all upto you how to use it.
Hi,
I can’t get the script above to css validate using v2.1 validation.
It keeps giving me this content type error. Can anyone please help? thank you!
The exact error:
URI : http://www.tsmgroup2.biz/css/dropdown/themes/default/dropdowncomb.php
-1 Unknown error org.w3c.www.http.HttpInvalidValueException: Invalid content type.
Feel free to use the validation service from w3.org
i think it make to load the css every time page loads
mAnisH, could you please tell me what did you do to load the css every time page loads? I could really need some help :(
I never stop learning in this business. I had no idea that css compression could be achieved in this manner.
Thank You!
Im using joomla with gzip but the css files are not compressed. I don’t use .htaccess bacause the server has freebsd so i would like to ask if your tutorial will help compress my css files or it will damage the website?
Thank you
I currently use this option create css.php into your assets and follow the code below
and paste it to your header
and its work…
Hi.
When i use the code like this :
then i have a compress file, but the css file (style.php) does not work on my page! any suggestion ?
i personally like the way of getting the hash of the php file.
like: style.php?r=md5 (file_get_contents(style.php)) to write it into the header.
so if file is the same, and users have cached it, it doesn´t need to be loaded again.
But if only 1 byte changed, every user has to redownload the file
Ooo!
Very good idea!
Why do we have to compress css files with PHP?
What is the reason?
Compressing CSS will result in lower loading time for your pages,
For eg:
a css file of 10kb would take more time and if same compressed css file is loaded it would definitely take less time.
bad practice for production.
compress your css then save it instead of compress it everytime the page loads.
how big is css anyway that you need to compress it, like, 999 Gigs?
Pretty helpful! :D
Great!
some another way to do this is running a php file (in some easter-egg url that developer knows) that compress .css or .js files after publish.
It can help in debugging process when designer needs to see line numbers in developer console.