Compress Your Web
In my never-ending quest to get my web pages to load super-fast, I chanced upon ob_gzhandler() one day while researching the uses of ob_start() in PHP.
What it does is, compress your html and web pages every time it's loaded onto a browser. Cool!
As usual, I will not get too technical on how it works but instead show you how to use it on your web pages like I have. And believe me, it's so simple you can even add it to your already existing pages without too much effort.
If it's not already obvious, you must be certain that you use PHP for your web pages and that your server supports it at least.
About PHP on your server
Then, you have to be sure that the version of PHP on your web server is extended with the zlib module. To find out quickly, just open NotePad and type in the following:
<?php
phpinfo();
?>
Then save the file as phpinfo.php and upload it to your webserver. Once you view this page on your browser, look for zlib on the web page. If it's installed, one table on the page would look this:

On that same page, just note that the PHP version on your web server is 4.0.6 or higher otherwise you can't use this method to compress your web pages.
Sample code to compress your web pages
To compress your web pages this is basically how you would do it:
<?php
ob_start( 'ob_gzhandler' );
?>
<html>
<head>
</head>
<body>
<p>This web page is now COMPRESSED!</p>
</body>
</html>
That's it! ![]()
THIS is why I LOVE PHP...
Is there another way to compress my PHP-driven web pages?
Sure there is, here's one more: Compress your web pages using ini_set('zlib.output_compression', '1').
Other info about compressing your web pages this way
Anyway, many agree that the
- speed gains are negligible,
- it reduces bandwidth...
I like!
- does not effect images (gifs / jpgs / pngs) since they're already compressed
- you can (add it to and) use .htaccess to make it work on your entire website alternatively.
Well, there's my first-ever article on PHP, if you have any comments or request for other non-technical articles on PHP, please feel free to do it on the GIDForums™.
