Jump to content

When should I be using ob_gzhandler?


cgm225

Recommended Posts

When should I be using ob_gzhandler?

 

When you want to send compressed output data to the user browser.

 

How should I be compressing my scripts?

 

It's not the scripts that are compressed. For that you should look at tools like Nu-Coder or eaccelerator (not really compress your scripts, but accelerate processing by caching their precompiled bytecode versions)

 

What you can indeed compress instead is the data that is sent to the user browser. This can be a whole webpage, part of it, or some other content that gets to be interpreted by the browser parsing engine.

 

You do this by using exactly the ob_ family of functions. ob_start() will turn output buffering on.

ob_gzhandler() should be passed to it as the first argument - the callback function - when you want the buffer contents to be compressed. This callback function will make sure your buffer contents are compressed according to the user browser specifications, or not compressed at all if the browser doesn't support it.

 

While output buffer is on, all data output (echo, print, and so forth) will not be sent from the script. It will be stored in the buffer. You then use ob_flush() or ob_end_flush(). The first sends the buffer contents (compressed if that was the case) and does not terminate output buffering. The second does the same thing but terminates output buffering, restoring output to its normal mode.

 

Before flushing output, if you want to keep the buffer stored somewhere you should use ob_get_contents(). This will ensure you get the buffer cached in a variable, because flushing the buffer will destroy its contents.

 

In a nutshell, this is it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.