PHP: gzip Compression
Posted by: Jordan in Programming, PHP on Jul 17, 2008
gzip is a compression algorithm similar to zip and rar formats. Using gzip to compress files before transmission can cause your page to load faster and save bandwidth on your server/webhost. Most (if not all) modern browsers support the gzip compression allowing you to utilize it in your PHP scripts.
How do you enable it?
It may sound like a lot of coding but PHP does most of the work for you. Enabling gzip compression is as easy as adding one line of code to your PHP script. Here is how its done:

Place ob_start("ob_gzhandler"); at the top of your PHP pages and those pages will automatically be compressed for browsers that support it.
PHP Manual Description
A Few Things to Note
How do you enable it?
It may sound like a lot of coding but PHP does most of the work for you. Enabling gzip compression is as easy as adding one line of code to your PHP script. Here is how its done:

- <? php
- ob_start("ob_gzhandler");
- ?>
- Compressed HTML page...
<? php
ob_start("ob_gzhandler");
?>
Compressed HTML page...
ob_start("ob_gzhandler");
?>
Compressed HTML page...
Place ob_start("ob_gzhandler"); at the top of your PHP pages and those pages will automatically be compressed for browsers that support it.
PHP Manual Description
ob_gzhandler() is intended to be used as a callback function for ob_start() to help facilitate sending gz-encoded data to web browsers that support compressed web pages. Before ob_gzhandler() actually sends compressed data, it determines what type of content encoding the browser will accept ("gzip", "deflate" or none at all) and will return its output accordingly. All browsers are supported since it's up to the browser to send the correct header saying that it accepts compressed web pages. If a browser doesn't support compressed pages this function returns FALSE.
A Few Things to Note
- zlib.output_compression is preffered over ob_gzhandler(). You cannot use this method and zlib.output_compression.
- This method is dependent on Apache and mod_gzip.
- IE5 and unpatched versions of IE6 may have problems rendering gzip compression.
