PHP compression without ob_start

Posted on 2007.09.23
Categories: PHP; Tagged with: , , ,

PHP compression is a popular feature that many have used over the years. Unfortunately, many have implemented the simple type of compression without reading what the online manual had to say - “note that using zlib.output_compression is preferred over ob_gzhandler().”

In this article, I will discuss the differences in implementation.

Gzip compression is now supported by most browsers. It makes sense that one would be using PHP’s built-in handlers to compress output, right? Save bandwidth and loading time?

Perhaps — the server is usually faster than the client, and therefore compression on the server side should be an unnoticeable lag. Decompression on the client side should also take milliseconds, and the total added time is hardly important, given the time saved sending the output over the Internet.

Unfortunately, most people now are taking the easy route - utilizing the ob_start() function, which buffers the output so that it is then compressed by PHP’s handlers. The entire process, on the programmer’s side, is only the addition of a line (unless they decide to implement additional if() checks).

I recommend the alternative, which is what the PHP Group recommends - zlib.compression. It is a setting that requires NO additional lines in any code, automatically detects whether the user supports it, and applies to all PHP files.

There is no apparent difference in the level of compression, but zlib.compression can also set an integer level of compression that translates into a speed/gain compromise. It seems — that is, to me, there is not an apparent measured difference — that no matter what level of compression is set, and how small the file size is, it takes about the same time. Once again, it is explained: the more resources that the server puts into compressing it, the less time is spent sending it. The less resources that the servers puts into compressing it, the more time is spent sending it.

To implement this type of compression, one needs to have either PHP 5.x installed as an Apache module or using the CGI interface, with the zlib library installed.

On the Apache module, it is as simple as a few lines in .htaccess.
php_flag output_buffering Off
php_flag zlib.output_compression On

and should you decide, a setting for the level of compression:
php_value zlib.output_compression_level "8"
Although I would say that a higher compression level might not make your hosts too happy. However, the compression level is a feature that is not well present in the ob_gzhandler method.

On the CGI interface, you must create a custom php.ini in your document root or the currently-working folder.
[PHP]
output_buffering = Off
zlib.output_compression = On
zlib.output_compression_level = “8″

If this doesn’t work for anybody, let me know using a comment, and make a “.phpinfo.php” file in your document root with the code:

By “doesn’t work”, I mean some drastic thing like PHP refusing to output anything - in that case, you’ll need to remove the customizations before your phpinfo will work. If you simply don’t notice much of a size difference, I suggest you verify that your browser supports it. I know that Gecko browsers (Firefox, Netscape, Mozilla) do.

You can also perform a Google search for other pages about zlib.compression.

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.5 out of 5)
Loading ... Loading ...

4 comments.

Pingback on November 19th, 2007.

[...] go using output buffering while utilizing our zlib.output_compression method mentioned in “PHP compression without ob_start“. (Strangely, that article was for not using ob_start, while this article is about using [...]

jcapshaw
Comment on February 16th, 2008.

Great article, finally got gzip working using the php.ini file. However, I was wondering how I should gzip other files such as html and css. I guess i could just use php and change the headers to .js, .css, or .html. What do you think?

Comment on February 16th, 2008.

Here’s how I do it on some of my web sites:

I use .htaccess to send JS, CSS, and HTML through PHP:
AddType application/x-httpd-php .js

Then, making sure that ALL of my JavaScript files are in a folder by themselves (or that the folder only contains JS files), I would create a php.ini file in that directory containing the customizations noted above in the article, and with the following additional line:
auto_prepend_file /home/yourusername/public_html/php.includes/js.inc.php

The js.inc.php file SIMPLY does the purpose of sending the appropriate header.

You’ve given me an excellent idea for a future article. Check back tomorrow. I may also include tips on using browser caching in the js.inc.php file to reduce bandwidth even further.

Pingback on February 17th, 2008.

[...] Firefox, JavaScript, PHP, toolbar, zlibToday, a newly-registered user “jcapshaw” noted (on an older blog post about using zlib for output compression in PHP) that he wanted to send [...]

Leave a comment

Names and email addresses are required (email addresses aren't displayed), url's are optional.

Comments may contain the following xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comments by unregistered users need approval, and all comments are subject to Akismet SPAM filtering. A valid e-mail address is required but will not be published or used for third-party solicitation (see our Privacy Policy). Please provide either your real name or a nickname representative of yourself.