Section 11.9. Compressing Output


11.9. Compressing Output

Output buffering allows you to compress the HTML you send to your visitors, which makes your site load faster for your users and also allows you to make more use of the bandwidth allocated to your server.

Whenever a visitor connects to your site, she sends along information such as the last page she visited, the name of the web browser she is using, and what content and encoding she accepts. The encoding part is what we're interested inif a browser supports compressed HTML, it sends word of this to the web server each time it requests a page. The web server can then send back compressed HTML if told to do sothis is important, because browsers that do not support compressed HTML will always get plain HTML back, so this works for everyone.

Compressed HTML is literally the zipped version of the normal HTML a browser would otherwise have received; the client unzips it, then reads it as normal. As zipping information requires that you must know all the information before you compress it, output buffering is perfectyou send all your data to a buffer, zip the buffer, and send it off to your users.

As the tie between output buffering and output compression is so close, the code to make it work is equally close. To enable it, just pass the ob_gzhandler parameter to ob_start( ); that will automatically check whether content compression is supported, and enable it, if it is. For example:

     ob_start("ob_gzhandler")     // output content for compression here     ob_end_flush( ); 

From the client's point of view, nothing will have changed, except the fact that the site might load a little quicker. If he clicks "View Source" from his web browser, he'll see normal HTML because the process is entirely transparent.

Content compression works only on the contents of the output bufferit does not compress pictures, CSS files, or other attachments to your HTML.

You're only allowed one compressed buffer with PHP because of the need to compress content all at once; be careful when stacking more than one buffer at a time.




PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net