views:

311

answers:

2

This is an odd one. I am slowly rebuilding a website on a live server. Some sections of the site have been rebuilt and therefore the code is placed in a subdirectory of the home dir (ie:/mysite/newcode).

I had successfully gzipped the old site using ob_start("ob_gzhandler"); So, i have applied the exact same code for the new code. However, for some odd reason, its returning as not gzipped. I have checked on http://www.whatsmyip.org/http_compression/ and http://www.gidnetwork.com/tools/gzip-test.php. I can't quite understand why it wouldn't be gzipping the new code if the gzip handler is included as one of the very first lines (before any output) on both old and new code.

PHP 5.1.6 Apache 2.0 Centos 5

A: 

http://docs.php.net/ob_gzhandler says:

Before ob_gzhandler() actually sends compressed data, it determines what type of content encoding the browser will accept ("gzip", "deflate" or none at all) and will return its output accordingly.
Could this be the cause of your problem?

edit: You can test this with something like

function dbg_ob_gzhandler($buffer, $mode) {
  error_log('dbg_ob_gzhandler invoked');
  $rv = ob_gzhandler($buffer, $mode);
  if ( false===$rv ) {
    error_log('client does not support compressed content');
  }
  return $rv;
}
ob_start('dbg_ob_gzhandler');
VolkerK
I have checked with Live HTTP Headers (in firefox 3.6) and im passing 'Accept-Encoding: gzip, deflate' to the server. So surely it should return compressed info. However, it appears the only thing that is getting compressed is a random css file.
David
I'd rather test that plus whether ob_gzhandler() is invoked at all on the server-side (if only because it's such a simple test) .
VolkerK
A: 

Found out the problem, not sure if documented anywhere...

If you use ob_start("ob_gzhandler"); and you what to flush your content, you must use ob_flush(), not flush(). Using flush will throw out the compression.

David
you should use ob_end_flush() or ob_end_clean() once you're done with the output
stillstanding
Mind me asking why?
David
I think the comments on the PHP docs explain it well. http://php.net/manual/en/function.ob-end-flush.php You can leave it out in a simple webpage request scenario and it will be called automatically. But PHP and these functions can be used in other scenarios. Also, if you decide to reuse your code in future, you are more likely to run into problems if you have not manually cleaned up.
Liam