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!
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 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 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");
// -------------------------------------------------------------------------------------
?>
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!
Post Feedback