How To Compress Your Pages With GZIP And PHP

Recently, we wrote an article which outlined 32 things that webmasters can do to speed up their websites. One item that we mentioned was the use of GZIP compression to speed up the delivery of web pages. Most people, however, hadn’t heard of GZIP, so we were contacted for methods to add it to websites.

In this article, we show how to use GZIP with PHP on a Linux Apache web server.

Does Your Web Host Provide GZIP Support?

Most web hosts compile PHP with support for GZIP. In fact, we have yet to find a Linux web host which does not support GZIP compression.

If you are in doubt about your web host, simply contact them and inquire about GZIP.

How To Add It To Your Website

Adding GZIP to your website is very simple. In fact, all you have to do is add one simply line of code to your php pages.

The code snippet below needs to be placed as the first line in your document, before any other code.

<?php ob_start("ob_gzhandler"); ?>

And that’s it. Now your web pages will be served more quickly.

Skeptical?

You might be wondering how that little line of code can make your web pages more efficient. And given that it is a PHP function, you might wonder why the added processing time does not affect the page load. However, GZIP is proven effective.

If you are skeptical, consider using a service such as WebOptimization.com to compare your load time before and after compression

 

More information about GZIP can be found in the PHP.net manual.

Tags: , ,

4 Responses to “How To Compress Your Pages With GZIP And PHP”

  • On May 29, 2010 at 10:20 pm,Jim wrote:

    Do you mean we have to place this code <?php ob_start(“ob_gzhandler”); ?> on the first line on all our php files, for Wordpreass on index.php, header.php, page.php, single.php, footer.php, sidebar.php, author.php, 404.php?

  • On December 3, 2010 at 6:24 pm,andy wrote:

    Hey there, thanks so SO! much for your help with all of these posts, they are amazing. I’ve copied and pasted the code into my own code, and it works like an absolute charm… Problem is though, it edits all of the font settings and a little of the layout of my site, which isn’t great. Any thoughts? Many thanks!

  • On July 27, 2011 at 10:13 am,angela wrote:

    this worked wonders on 2 of my client’s websites but it showed a blank white page on 1 other site. All 3 sites are wordrpess but I’m wondering if it has to do with how the theme is coded on the one that failed. Any ideas? Thank you for this tip though it worked wonders on the other 2!

  • On July 28, 2011 at 6:08 am,Velvet Blues wrote:

    @Angela: I haven’t seen the white page… If the theme works fine without being compressed, I suspect that this could be something to do with the hosting configuration.

Trackbacks

Trackback URL for this entry:
http://www.velvetblues.com/web-development-blog/how-to-use-gzip-with-lamp/trackback/

Leave a Reply

Want us to work on your project?

Contact us today for a quote. Click here to submit details regarding your project.

If you are making a general inquiry, send an email to info@velvetblues.com