Gzip encoding your php

I just started using ob_gzhandler to send gzip encoded/compressed pages to browsers that accept it. The speed change seems pretty significant and pages appear to load instaneously. I followed the instructions on David Child's site, but if you can't use ob_gzhandler, you might try the gzip_encode.php class.

The .htaccess method is simple:

1) In your .htaccess file:

php_value auto_prepend_file /full/path/to/begin_gzip.php
php_value auto_append_file /full/path/to/end_gzip.php

2) The begin_gzip.php file:

<?php ob_start("ob_gzhandler"); ?>

3) The end_gzip.php file:

<?php ob_flush(); ?>

What this does is tells the server to prepend the begin_gzip.php file to the beginning and append the end_gzip.php to the end of every php file it serves. This is such a simple step to take and the impact might be great for circumstances including access via dial up or mobile devices.

If you're interested in seeing what difference gzipping will make to your pages, check out Leknor's test for gzip encoded pages. What's really wonderful is that I tested the iainstitute.org site and found that our web host must already have the server configured to gzip php pages. Very cool.

Here's the testing information for the urlgreyhot weblog page before the site was gzipped:

http://urlgreyhot.com/personal/ is not gziped. If it were gziped the requested page (65049 bytes) would be the following sizes at:

Level bytes % of orig size 1k/sec 3.5k/sec 10k/sec 100k/sec utime
0 65065 100.0246 63.5 18.2 6.4 0.6 0
1 22606 34.7523 22.1 6.3 2.2 0.2 1
2 21724 33.3964 21.2 6.1 2.1 0.2 1
3 21258 32.68 20.8 5.9 2.1 0.2 2
4 20214 31.075 19.7 5.6 2 0.2 2
5 19717 30.311 19.3 5.5 1.9 0.2 3
6 19560 30.0696 19.1 5.5 1.9 0.2 4
7 19547 30.0497 19.1 5.5 1.9 0.2 3
8 19543 30.0435 19.1 5.5 1.9 0.2 4
9 19533 30.0281 19.1 5.5 1.9 0.2 4

The 1k/sec column is the time to download the compressed version at 1024 bytes per second.