PHP Compressing Output

Recent browsers support compressing the text of web pages; the server sends compressed text and the browser decompresses it. To automatically compress your web page, wrap it like this:


The built-in ob_gzhandler() function can be used as the callback for a call to ob_start(). It compresses the buffered page according to the Accept-Encoding header sent by the browser. Possible compression techniques are gzip, deflate, or none

It rarely makes sense to compress short pages, as the time for compression and decompression exceeds the time it would take to simply send the uncompressed text. It does make sense to compress large (greater than 5 KB) web pages

Instead of adding the ob_start() call to the top of every page, you can set the out put_handler option in your php.ini file to a callback to be made on every page. For compression, this is ob_gzhandler

Leave a Comment