Gzip encoding your php

I just started using ob_gzhandler to send gzip encoded/compressed pages to browsers that accept it. The speed change seems pretty significant and pages appear to load instaneously. I followed the instructions on David Child's site, but if you can't use ob_gzhandler, you might try the gzip_encode.php class.

The .htaccess method is simple:

1) In your .htaccess file:

php_value auto_prepend_file /full/path/to/begin_gzip.php
php_value auto_append_file /full/path/to/end_gzip.php

2) The begin_gzip.php file:

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

3) The end_gzip.php file:

<?php ob_flush(); ?>

What this does is tells the server to prepend the begin_gzip.php file to the beginning and append the end_gzip.php to the end of every php file it serves. This is such a simple step to take and the impact might be great for circumstances including access via dial up or mobile devices.

If you're interested in seeing what difference gzipping will make to your pages, check out Leknor's test for gzip encoded pages. What's really wonderful is that I tested the iainstitute.org site and found that our web host must already have the server configured to gzip php pages. Very cool.

Here's the testing information for the urlgreyhot weblog page before the site was gzipped:

http://urlgreyhot.com/personal/ is not gziped. If it were gziped the requested page (65049 bytes) would be the following sizes at:

Level bytes % of orig size 1k/sec 3.5k/sec 10k/sec 100k/sec utime
0 65065 100.0246 63.5 18.2 6.4 0.6 0
1 22606 34.7523 22.1 6.3 2.2 0.2 1
2 21724 33.3964 21.2 6.1 2.1 0.2 1
3 21258 32.68 20.8 5.9 2.1 0.2 2
4 20214 31.075 19.7 5.6 2 0.2 2
5 19717 30.311 19.3 5.5 1.9 0.2 3
6 19560 30.0696 19.1 5.5 1.9 0.2 4
7 19547 30.0497 19.1 5.5 1.9 0.2 3
8 19543 30.0435 19.1 5.5 1.9 0.2 4
9 19533 30.0281 19.1 5.5 1.9 0.2 4

The 1k/sec column is the time to download the compressed version at 1024 bytes per second.

Comments

01 INnUP Sponsoren Space
05/01/06 @ 09:37

Thx a lot for your blog. However, I do not yet understand what to do in my case (Check out the link to my site and you´ll see that there is some html but a huge amount of js-stuff that is included). Will that be gzipped as well?

I already gzipped the js-files and put them on the server but nothing happened.

Thx for your help
Phillip

02 jibbajabba
05/01/06 @ 12:53

I believe any data in the file that you serve using the above method will be gzip encoded.

Advertisement
03 Joe
09/22/06 @ 15:00

I did a quick test to see how many other popular websites are using gzip and was surprised to find most are not.

Only a handful of sites providing external scripts (like analytics code) are providing gzip when requested by browsers. *Google* and *MyBlogLog* do it. *Site Meter*, *Webtrends* and *Performancing* do not.

In fact, some sites claiming to teach how to make good websites have home pages that are **over 350k!**

If you're a WordPress / PHP / JavaScript user, or want to do this on a windows machine with IIS6, you might also like the write-up I did at VibeTalk:

http://www.vibetechnology.com/vt/2006/09/22/triple-website-performance-with-gzip/.

I inlcuded a table listing the sizes of 10 popular websit home pages that may surprise you.

If anyone has questions on making this work, I'm happy to help!

-Joe

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <strong> <dd> <dl> <dt> <i> <li> <ol> <u> <ul> <code> <blockquote> <fieldset> <legend> <h6> <span> <img> <div> <p>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options