Bigsmallbig.

Thanks in part to a nudge from Mark Pilgrim, and in part to a smart little PHP class (long since disowned by its author) [see below], the documents on this site are now served gzip compressed (at sizes reduced as much as 75%) to browsers which can handle such data, which I believe is most, though I’m not sure, but I’m fairly sure that browsers which cannot handle such data are sent a plain uncompressed file. I would ask for reports of decompression errors, but if there were any decompression errors the request wouldn’t be visible; such is the nature of compression. If you can read this it works.

UPDATE: Anyone with a recent version of PHP (4.1+) running on their server can easily take advantage of this. Benefits are greatly reduced bandwidth consumption and a much speedier site, especially for readers using modems.

Simply place the following snippet at the very first line of PHP-aware web pages, before :

And that’s it. Test here.

Potential problems and solutions:

  • If compression is failing and you can see the code snippet when you View Source on your page, the page is not PHP-aware. Create a text file in your web directory called .htaccess (if one exists already, open it) and paste the following inside it:
    AddType application/x-httpd-php .html .htm
  • If compression is failing and you can’t see the code snippet when you View Source, chances are your server’s version of PHP was compiled without the gzip library. Confirm this with your server admin.
  • If you get the error message "output handler 'ob_gzhandler' cannot be used twice", your pages are already being compressed. Felicitations.
  • If you get an error message like "headers already sent", make sure the snippet is at the very beginning of the page, before anything else.

[Textism]