Speed Up and Save Your Website Bandwidth with GZip Compression
Why GZip compression can speed up your website?
With GZip compression enable it will compress data being sent out from your Web server, and have the browser decompress this data on the fly, thus reducing the amount of data sent and increasing the page display speed.
Why Compress HTML?
HTML is used in most Web pages, and forms the framework where the rest of the page appears (images, objects, etc). Unlike images (GIF, JPEG, PNG) which are already compressed, HTML is just ASCII text, which is highly compressible.
How can i enable gzip compression?
You can install the apache mod_gzip module if you are familiar with server. Or, you can use PHP ob_gzhandler() function to achieve the same output.
The code below is getting from PHPBB forum:-
Save this file as “gzip_header.php”
< ?php
$phpver = phpversion();
$useragent = (isset($_SERVER["HTTP_USER_AGENT"]) ) ? $_SERVER["HTTP_USER_AGENT"] : $HTTP_USER_AGENT;if ( $phpver >= ‘4.0.4pl1′ && ( strstr($useragent,’compatible’) || strstr($useragent,’Gecko’) ) ) {
if ( extension_loaded(’zlib’) ) {
ob_start(’ob_gzhandler’);
}
} else if ( $phpver > ‘4.0′ ) {
if ( strstr($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], ‘gzip’) ) {
if ( extension_loaded(’zlib’) ) {
$do_gzip_compress = true;
ob_start();
ob_implicit_flush(0);
header(’Content-Encoding: gzip’);
}
}
}
?>
Save this file as “gzip_footer.php”
< ?php
$gzip_contents = ob_get_contents();
ob_end_clean();$gzip_size = strlen($gzip_contents);
$gzip_crc = crc32($gzip_contents);$gzip_contents = gzcompress($gzip_contents, 9);
$gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
echo $gzip_contents;
echo pack('V', $gzip_crc);
echo pack('V', $gzip_size);
?>
How to use?
< ?php
include('gzip_footer.php');
include('your_template_file.php');
include('gzip_footer.php');
?>
Pros:
- page being compress and now have smaller file size
- It takes shorter time for user to download your file
- save more bandwidth
Cons:
- not all browser support gzip compression (most of the new browser support gzip)
- it takes a bit of server resources to do the compression
Credit: Webreference
Posted at September 14th, 2006 by chua
If you think this article helps you to solve your problem and clear your headache, feel free to buy me a drink :)









September 14th, 2006 at 11:52 pm
Cheers for this! will check it out durin the holidays.
September 22nd, 2006 at 8:41 am
this is really worth for trying.
I’ve a page with original size 92k,
after turn on the compression the size squeeze to 12k
it’s really save a lot of bandwidth!
November 25th, 2006 at 6:15 pm
chear .. i must use this one in my vietnamese news, and other my portal forum site.. thanks
August 8th, 2007 at 4:49 pm
Hello! Help solve the problem.
Very often try to enter the forum, but says that the password is not correct.
Regrettably use of remembering. Give like to be?
Thank you!
November 23rd, 2007 at 6:51 pm
[...] Compress your HTML with GZip Just like the CSS, your HTML can contain a lot of whitespaces and useless breaks. If you have a PHP website, try the GZIP compression. This will trim-down your filesize big time. How to implement and enable GZIP, check the tutorial on techiecorner.com. [...]