Learning Journal

Compress Your Web Page

In my never-ending quest to get my web pages to load super-fast  I chanced upon ob_gzhandler() one day while researching the uses of ob_start() in PHP.

What it does is compress your markup/HTML (web page) every time it is requested by a browser capable of handling compressed web pages.

As usual, I will not get too technical on how it works but instead show you how to use it on your web pages like I have. And believe me, it’s so simple you can even add it to your existing pages without too much effort.

If it’s not already obvious, you must be certain that you use PHP for your web pages and that your server supports it at least.

About PHP on your server

Then, you have to be sure that the version of PHP on your web server is extended with the zlib module. To find out quickly, just open a text editor and type in the following:


Then save the file as phpinfo.php and upload it to your web site. Once you view this page with your browser, look for the zlib section on the web page. If it’s installed, one table on the page would look like this:

zlib PHP Extension

On that same page, just note that the PHP version on your web server is 4.0.6 or higher, otherwise you can’t use this method to compress your web pages.

Sample code to compress your web pages

To compress a web page, this is basically how you would do it:

ob_start( 'ob_gzhandler' );
        <p>This web page is now COMPRESSED!</p>

That’s it!

This is why I LOVE PHP… 🙂

Is there another way to compress my PHP-driven web pages?

Sure there is; here’s one more: Compress your web pages using ini_set('zlib.output_compression', '1').

Enable compression on your entire web site

With Apache Web Server, you may easily serve compressed web pages every time and everywhere on your web site by following the instructions in this document: Compress Your Web Site.

Other general information about compressing your web pages

Many people agree that

  • speed gains are negligible,
  • it reduces bandwidth… I like!
  • it does not effect images (gifs / jpgs / pngs) since they’re already compressed
  • you can (add it to and) use .htaccess to make it work on your entire web site alternatively.

Well, this my first-ever article on PHP, if you have any comments or request for other non-technical articles on PHP, please feel free to do it on the GIDForums.

2 replies on “Compress Your Web Page”

Leave a Reply

Your email address will not be published. Required fields are marked *