Web Content Compression (How To Make Your Visitors Even Happier)
Now that you have pushed your PHP application's performance to the top :-) it's time to work on
one of the other factors that can make your site seem slow to visitors: download speed. If your
application is used in your Intranet, with all of your clients using a 100Mb/s ethernet connection
to the server, this probably may not be a problem. But if you have to serve slow modem clients,
then the solution is content compression. Most browsers support content compression with gzip, according
to IETF standards. This means that you can send web content to a browser compressing it with gzip,
and the browser will transparently uncompress the data and make the page visible to the user.
There are different methods you can use to compress web content.
mod_gzip is an Apache module available for free from
that can compress static web content to browsers that support this kind of
Content-Encoding. It really works fine with most of the static content, and you can easily
compile it with apache (or use it as a DSO). Folks
at Remotecommunications say that it could compress dynamic content also, coming from mod_php, mod_perl,
or mod_whatsoever :-). I tried and tried, but this seems not to work. I saw many postings in mod_gzip mailing
list, and it seems that in the next release (220.127.116.11f, I suppose) this will be fixed. Meanwhile,
you can use it for the static part of your site.
But we want to compress the dynamic content also, so we have to find other ways. A solution is to use
, a PHP class that you can use to compress your pages
by calling some of its functions at the very beginning and end of your PHP scripts. A site wide solution
is to call these functions from the auto_prepend and auto_append directives of your php.ini file.
This works fine, but obviously introduces a bit of overhead in heavily loaded sites. For details on how
to use it, look inside the class code (at least you need PHP compiled with zlib support).
It's very well commented and the author tells you everything you need to know.
Since PHP folks' duty is to surprise me every day, I read here
an interesting article from our friend Zeev Suraski (shame on you if you don't know this PHP guru ;-) ) about
output buffering with PHP. It also tells us that in PHP 4.0.4 a new output buffer handler has been introduced,
ob_gzhandler, which does just the same thing as the above class. But instead of the previous class, you can
put this in your php.ini, using the following syntax:
output_handler = ob_gzhandler ;
This makes PHP activate output buffering and compress anything it is sending out. If you have particular
reasons for not putting it here, you can always change the default behaviour (not compress) with a .htaccess
file in your to-be-compressed PHP source directory, using this syntax:
php_value output_handler ob_gzhandler
... or even call it from your PHP code, in this way:
Output buffering and compression is discussed also in this
PHPBuilder.com article from Luis Argerich. The output buffer handler approach really works fine, and doesn't introduce particular
overhead on the server. I really suggest you to use this approach. Your 28.8K modem users will think that
they suddenly got a ISDN line for Christmas, and probably will offer you a drink next time they meet you ;-) .
Pay attention: Netscape Communicator doesn't like compressed images, and it will display them as broken. So
you have to disable compression for jpegs and gifs, unless your users are all using Internet Explorer (which
works just fine). In general compression should work for all other files, but I suggest you to test each browser,
particularly if you use "strange" plugins or viewers.