Speed Up and Save Your Website Bandwidth with GZip Compression

advertisement

php, open source, open source software 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_header.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

Technorati Tags: , , , , , , , , ,

Related posts:

How to write robot.txt to control search engine spider
Track Your Website Visitor With Free Invisible Website Tracker - StatCounter
Password protect directory with .htaccess and .htpasswd
Symfony: How to escape hash # character in yaml
WordPress: How to remove meta generator tag?
How to loop thru directory files in bash shell
How to clear play history in VLC media player
How to add native menu support in your WordPress theme?






6 Responses to “Speed Up and Save Your Website Bandwidth with GZip Compression”

  1. MM Nauman says:

    Thank you man i was not knowing anything about gzip thanks for this post but how to use gzip

  2. [...] 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. [...]

  3. AltaGid says:

    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!

  4. chear .. i must use this one in my vietnamese news, and other my portal forum site.. thanks

  5. chua says:

    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!

  6. Alex says:

    Cheers for this! will check it out durin the holidays.

Leave a Reply