Compressing your page
Want to save some bandwidth for your site? Want to help those poor visitors who visit your page using 36.6k internet speed while your HTML document itself is already over 300KB and you still got tons of flash/graphic ads waiting to bombard them? Well, maybe you're not running hated popup ads website, but isn't it a good idea to compress your HTML content if it's possible? A lot of files that you're familar with are presented in compressed/encoded format, example: JPG, MP3, ZIP and etc. So why can't HTML document being served in compressed form, since most of the browser you can find right now supports that?
Quick Facts
Below is the comparision on gzip-encoded and original page size on my site.
Compressed vs Un-compressed
| File |
Original (bytes) |
Compressed (bytes) |
| index.php |
84,722 |
21,933 |
| /misc/weather.php |
132,062 |
14,785 |
You won't gain much benefit if your page size is small. However, if you have lots of pages and/or huge content, this will save your server heaps of bandwidth. It will do good to your server too when search engine bots are crawling your site (only if they are able to accept gzip encoded data). Client-side-wise, the visitor will be able to download and load your document faster too. The benefit is even more obvious when you're browsing a page that is 4MB+ size that contains huge amout of information in text format (for example, dumping/checking log files). With gzip compression enabled, the page might be compressed to 300KB instead of 4MB. Which is faster? 300KB or 4MB?
PHP: ob_gzhandler()
PHP does include functions that does the job for you. However, it really depends on the PHP version and how your hosting server set it up. This article will use PHP ob_gzhandler() to gzip-encode your webpage.
Note:
ob_gzhandler() requires the zlib extension (mod_gzip module). It is also not available in versions of PHP previous to 4.0.5.
According to PHP manual, you cannot use both ob_gzhandler() and zlib.output_compression. Also note that using zlib.output_compression is preferred over ob_gzhandler().
The Code Is Simple
1) Place this at the very top of your php files:
<?php ob_start("ob_gzhandler"); ?>
2) Place this at the end of your php files:
<?php ob_flush(); ?>
Note: It is not necessary for you to call ob_flush(). PHP will automatically do it end the end of script execution.
What's in the background
Once you've this setup, whenever somebody request your page with "Accept-encoding:gzip" (or deflate) in their HTTP request header, the server will automatically reply with "Content-Encoding: gzip" in the HTTP reply header, and send the gzip-enconded page to the requester.
But if the requester didn't state that they can accept gzip encoding, then the server will serve the uncompressed content. This is done automatically in the background. In other words, almost all web browsers supports compressed web pages and they will request/accept gzip encoding without your knowledge. You can check the HTTP request header sended by web browser to understand more.
More example
Below is an example on how to use the above codes with header() and sessions.
<?php
// GZip the buffered output if zlib module
// is enabled
if(extension_loaded('zlib'))
ob_start('ob_gzhandler');
// Note: Let PHP automatically call ob_end_flush()
// at the end of script execution
// We need this to serve content with
// 'true encoding'
header('Content-Type: text/html; charset=utf-8');
// required for access to POST/GET/SESSION/COOKIE
session_start();
?>
Using .htaccess method
There is another way to do this without adding the codes to every php file. We can achieve this by using htaccess.
In your .htaccess file, include the following lines:
php_value auto_prepend_file /path/begin_gzip.php
php_value auto_append_file /path/end_gzip.php
In your begin_gzip.php file:
<?php ob_start("ob_gzhandler"); ?>
In your end_gzip.php file:
<?php ob_flush(); ?>
What it does is it tells the Apache 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 will saves you the trouble for adding the codes to every php file, but it won't suit everyone.
Besides using htaccess method, you can use include() or require() to include the php script.
What you need to becareful of
Be sure there is nothing, I mean no html, no white spaces or anything before you call ob_start('ob_gzhandler'). If not you'll end up with a stream starting with un-compressed content and followed by gzip compressed data, which browser will 100% failed to recognize and they will not uncompressed it.
Last update: 2007.06.14