Gzip Compression
Listed In PHP and MySQL » Database Interaction — Viewing Full TutorialFor this tutorial you will need:
- A webserver with PHP 4 or above (recommended)
- Gzip and Zlib enabled on PHP
- A PHP site. The bigger the site is = the more bandwidth used = the bigger the difference when you use this tutorial.
What's Gzipping?
Gzipping is a method of compressing your pages so they load faster. You can gzip all sorts of file, including webpages and CSS. This tutorial will show you how to compress your .php extended web page.
Let's start off
Open up your page, and before any HTML output or echo add this line of code:
<?php
ob_start("ob_gzhandler");
?>Now at the end of your page, add this code, which sends the buffer to the browser:
<?php
ob_end_flush();
?>That's it! Your page should now load much faster. If you want to check if this is working properly, you can enter your page URL here: http://www.whatsmyip.org/mod_gzip_test/
Going further...
If you want extremely fast page loading then you can tweak with PHP's ini file temporarily. Instead of the suggested code, you could use this:
<?php
ini_set('zlib.output_compression_level', 9);
ob_start("ob_gzhandler");
?>
You can change the number in ini_set() to anything from 1-9, 1 being the least compression to 9 being the most.
WARNING: Using too much compression may hurt your server load and results could be degraded.
