5ubliminal@twitter

Easy Method To Save Bandwidth For PHP Coders : 5ubliminal's TellinYa

<a href="http://www.tellinya.com/art2/106/">Easy Method To Save Bandwidth For PHP Coders : 5ubliminal's TellinYa</a>
5ubliminal's YAMS

I decided to use output buffering and added gzip compression to the site. Pages load much faster and sizes in access log are 3-5 times smaller then before.

I'm not sure if this puts strain on the server but I'll add page caching too very soon and I'll make sure I won't worry when real traffic starts to pour in!

Methods of adding gzip compression

There are actually two methods of doing gzip compressions of your web pages and both include output buffering.

The two methods can be labeled : the easy way and and the hard way! And, guess what, I chose the hard way!

The easy way and downsides

<?
// The next lines sets gzip compression on!
ob_start("ob_gzhandler");
?>
<html>
<body>
<p>The HTML comes in after the PHP instruction and will be gzipped.</p>
</html>
<body>

This is the easy method because … it's easy! One line of code added before all your PHP files does the trick. Yes, but … you can not use output buffering for internal use anymore! If you right click this page and look at the HTML you'll see it's not looking good. I do it on propuse to make it hard to read ;)

So the use of this method would disable the possibility to play around with HTML before actually delivering it. And I love to play.

The hard way … my way!

The hard way looks pretty much like the one above but you have to do the dirty work yourself!

<?
// -------------------------------------------------------------------------------------
$EnableGZipEncoding true;
// -------------------------------------------------------------------------------------
// Helper function to detect if GZip is supported by client!
// If not supported the tricks are pointless
function acceptsGZip(){
    
$accept str_replace(" ","",
        
strtolower($_SERVER['HTTP_ACCEPT_ENCODING'])
    );
    
$accept explode(",",$accept);
    return 
in_array("gzip",$accept);
}
// -------------------------------------------------------------------------------------
function playWithHtml($OutputHtml){
    
// This will mess up HTML code like my site has done!
    // View the source to understand! All ENTERs are removed.
    // If your site has PREformated code this will break it!
    // Use regexp to find it and save it and place it back ...
    // or just uncomment the next line to keep enters
    // return $OutputHtml;
    
return preg_replace("/\s+/"," ",$OutputHtml);
}
// -------------------------------------------------------------------------------------
function obOutputHandler($OutputHtml){
    global 
$EnableGZipEncoding;
    
//-- Play with HTML before output
    
$OutputHtml playWithHtml($OutputHtml);
    
//-- If GZIP not supported compression is pointless.
    // If headers were sent we can not signal GZIP encoding as
    // we will mess it all up so better drop it here!
    // If you disable GZip encoding to use plain output buffering we stop here too!
    
if(!acceptsGZip() || headers_sent() || !$EnableGZipEncoding) return $OutputHtml;
    
//-- We signal GZIP compression and dump encoded data
    
header("Content-Encoding: gzip");
    return 
gzencode($OutputHtml);
}
// This code has to be before any output from your site!
// If output exists uncompressed HTML will be delivered!
ob_start("obOutputHandler");
// -------------------------------------------------------------------------------------
?>

Difference between the Easy way and the Hard way

The first code with its one line does everything seconds does. The second does what the first does … with a twist. It allows you to alter the Output before dumping it to the client. In the hard way you have to check if GZip is supported, if headers are not sent and only then send data. The easy way does it automagically :)

Notice: $EnableGZipEncoding config variable present on top of the code! If you set it to false you will no longer output GZip even if possible. You can use the above code just to alter output html.

Read the benefits of output buffering tutorial to understand more and see what you can do with output buffering!

6 Comments Posted By Readers :

Add your comment
#1 Frederick from Canada web
Posted on Sunday, 23 September, 2007
Hi. I'm more of a zlib person - I like to compress my output using zlib.compression rather than ob_start("ob_gzhandler"). The PHP Group recommends it - so I'm going along with it.

My article http://www.geekie.org/technology/software/php/2007.09/47.php-compression-without-ob_start.web tells people how to implement zlib.compression.
#2 Photos China from China web
Posted on Friday, 09 May, 2008
It work perfectly !


Thanks
#3 5ubliminal web
Posted on Friday, 09 May, 2008
You're welcome:)
#4 Rait Raidma from Estonia
Posted on Thursday, 15 May, 2008
Hi! I have a module for Firefox called HTML Tidy and it has an option "Clean up the page..." which removes your "hack" with function playWithHtml() and the source is again well readable.
So, if you whant to protect your source you should come out with something else.
#5 5ubliminal web
Posted on Thursday, 15 May, 2008
I don't want to protect my source :) I just want to make it hard to read.
I could do more but I want the search engines to index me… right?:)
#6 ali from Iran, Islamic Republic Of
Posted on Friday, 19 September, 2008
hi,

thank you for HARD WAY , it work great on bandwidth saving.
Post Feedback 
Name *
Mail *
URL
« Anti-Spam
» URL will only go live after a review. Comments are moderated. «
5ubliminal's TellinYa.com SEM & SEO Blog © 2007 - All rights reserved unless mentioned otherwise .
Rendered On : [Friday, 21 November, 2008 - 09:53:21 GMT]   No Ajax / Flash Used Here
" Easy Method To Save Bandwidth For PHP Coders : 5ubliminal's TellinYa "