ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

.htaccess Wrappers with PHP

(Page 4 out of 6)

To make the HTTP compression work, we use two functions: ob_start() and ob_gzhandler(). Output buffering functions are strange. Any time you try to display something, you can have PHP save up everything you're trying to output. At the very end it's all dumped into a function of your choosing where the text can be changed or transformed before it's output.

There is a built-in PHP function called ob_gzhandler() which takes one parameter (a string of text), compresses the data according to the gzip standard and does all the header trickery that's needed to tell the user's browser that we are transmitting data that needs to be decompressed once it's downloaded. When this line is used:

ob_start("ob_gzhandler");

It tells PHP: everything displayed afterwards has to go through the function ob_gzhandler() first. Put that at the top of our script and here's what we've got:

Save that as compress.php. Upload both files, chmod htaccess.txt to 0755 and rename to .htaccess and you're done. That's all you need for it to work, and you can just as easily apply HTTP compression to any script by just adding that line.

To try this puppy out, I got on a dialup connection and put a copy of "The Decline And Fall Of The Roman Empire Volume 1" on my web host, a 900 page book, about 1.6 megabytes in size. Without HTTP compression it took 5 and a half minutes to download. With the compression, only 2 minutes. Internet Explorer told me the download was going at 20 KB per second, impossible with a dialup connection... but since the file was zipped, I really was downloading 20 KB a second (once the data was decompressed on my end) over a 5 KB per second connection.

Though HTTP compression will work on sounds, video, and images, the space you save is negligible, usually only a few bytes. These sorts of media are already heavily compressed so zipping makes almost no difference. This is why we've told htaccess to only use compression on text and HTML, because it's with human languages like English where a lot of repetition occurs, which means more information can be compressed.

Not all browsers support HTTP compression, but ob_gzhandler() figures out if a browser can support HTTP compression. If the browser doesn't, the original file is displayed, no harm done.

You can get a copy of this sample script at: http://www.jumpx.com/tutorials/wrapper/compress.zip

Both of these scripts I've created for you will work only on static files, files that actually exist such as images or HTML files. If you tried to apply these wrappers as-is to PHP scripts, Perl scripts or even HTML pages that use SSI. If your whole site is run by a single script it's a better idea to hard-code these things right in, anyway.

« Previous: Shrink-Wrap
Next: The best thing since bubble wrap »



Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction
  2. Tortilla Wrap
  3. Shrink-Wrap
  4. "Gift Wrapping" Your Output
  5. The best thing since bubble wrap
  6. The Wrap-Up
Bookmark Article
Download Article
PDF
Download this article as a PDF file