Below is what I regard as a reliable method of doing gzip output compression.
One thing that normally breaks gzip compression is something printing/echoing before the ob_start (usually the result of an error, debug output, errant space somewhere etc…), this is where the ob_get_length and ob_flush come in.
The problem is that headers_sent is not a reliable way to tell if any thing has been output because it may still be in the output buffer, so if we flush it before hand problem solved.
<?php
function doPage()
{
echo 'Hello World';
}
if (ob_get_length() > 0)
@ob_flush();
if (!headers_sent() && @ob_start("ob_gzhandler"))
{
doPage();
ob_end_flush();
//Probably a good idea to exit here as more output would break it
exit;
}
else
{
doPage();
}