Compression – Part II: PHP’s ob_gzhandler()

11 04 2010

Recently, I discussed minification and today I’m going to examine using PHP to compress output using the ob_gzhandler(). There are various ways to accomplish this for browsers that support decompressing content, such as Firefox 3.6. You may use it as a callback to ob_start() by writing a short script as follows and include it at the start of a PHP script:


Alternatively, if you can modify the PHP INI file you could alter to it so it has the following settings:

output_buffering = On
output_handler = ob_gzhandler

The first way is beneficial if you only want selected PHP files to be compressed, whereas changing the settings of the INI file causes the settings to be applicable for all PHP files.

You may alternatively wish to create a .htaccess file with these instructions:

php_flag output_buffering On
php_value output_handler 'ob_gzhandler'

If you have access to the Apache’s httpd.conf, you could avoid creating a .htaccess file and thereby speed things up by adding the following to the Apache configuration file:

<IfModule mod_php5.c>
php_flag output_buffering on
php_value output_handler 'ob_gzhandler'

Parenthetically, if you are confused about when to use php flag vs. php value for either a .htaccess file or for the Apache httpd.conf, see

If you wish to use a .htaccess file or issue PHP commands from within the httpd.conf, typically PHP would run as a module of Apache.

You may want to give ob_gzhandler() a try as it will detect what kind of encoding the browser will accept, i.e. gzip or deflate encoding and then for browsers supporting either of these types, the function returns compressed content with the proper encoding.

According to the PHP manual, ob_gzhandler() requires the zlib extension. For those running linux, this means that you would build your PHP by enabling the extension –with-zlib[=DIR] while for those on windows support for that extension is built into PHP ( If you wish to verify that you indeed have that extension, you may wish to write a script using phpinfo() which lists all the details about your php configuration. When you run the script you should see something similar to the following:

zlib info from phpinfo()

There is also yet another way to use PHP to compress content that is even better, and I will explore that option soon.

To check that a PHP file is actually being sent compressed and that the browser will decompress it, I use the Firefox extension LiveHttpHeaders which you can set up to run as a sidebar. I look for a request header like the following:

Accept-Encoding: gzip,deflate

with a response header:
Content-Encoding: gzip (or deflate)

This work is licensed under a Creative Commons License




One response

12 04 2010
Creating Images Fast and Easy on Windows « Sharon Lee Levy's Blog

[…] PHP to create images, but I’m going to explore what I did today to create an image to enhance my latest post about compression using […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: