What is the most efficient way of auto gzipping css and js with PHP?

I was curious in the most efficient way to have my outputted .css and .js files automatically gzipped on the server using PHP. I have come across a few methods and I am wondering what is the more efficient and preferred method:

1. Modify .htaccess file to auto_prepend .php file


AddHandler application/x-httpd-php .css
php_value auto_prepend_file gzip-css.php
php_flag zlib.output_compression On

and the gzip-css.php file is:


<?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);
?>

2. Manually attach a PHP file to each CSS call

For example, gzip.php?file=/style.css with code like the above gzip-css.php or like Minify from http://code.google.com/p/minify/. Minify goes the extra step into combining .css files I select and puts them into one HTTP request.

Does the web browser cache the .css or .js file the same way if I were to direct link a .css or .js file in my head or do I have to be more cautious?

None of them will be more efficient then the other. Both would end up doing the same thing. It would be far more efficient to let the web server handle .css and .js and cache those results for future connections. Honestly you want to avoid doing things like sending CSS to the PHP parser or file. While in development it is handy, but in production convert your multi-css files into one before you upload. Do not combine them dynamically.

i compress js using some compressors(must have almost same size…)
but i dont compress css…
and i dont do it using php…that way may be we dont need to be worried either particular server supports it or not

Turning on the appropriate Apache module is more efficient than doing it manually since once it is turned on you don’t have to think about it at all.

Even easier is where the web hosting provider turns it on for you so you don’t even have to do that - particularly if they have implemented code so that it only runs when running it will make the download faster.

Are you suggesting the mod_deflate method?

Well that is the most modern of the options to do what this thread is discussing.