What is the difference between using ob_start()
and ob_start('ob_gzhandler')
?
How does it affect the page speed ?
The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.
While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer. The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush().
Using ob_start allows you to keep the content in a server-side buffer until you are ready to display it. This is commonly used to so that pages can send headers 'after' they've 'sent' some content already (ie, deciding to redirect half way through rendering a page).
the ob_gzhandler is a callback function which takes the contents from your output buffer and compresses the data before outputting it. This reduces the size of the content being sent to the browser which might speed up the content transfer to the client.
This doesn't affect page speed in the sense you'd might think.
the ob_gzhandler is a callback function which takes the contents from your output buffer and compresses the data before outputting it.
This reduces the size of the content being sent to the browser which might speed up the content transfer to the client. But it doesn't speed up your application/website.
I needed to force gzip for some admin pages (full of complicated HTML tables) that weren't being automatically compressed for some clients so I added this method. I'm not sure I would force it for every page, but at least the admin pages its fine.
function force_gzip()
{
// Ensures only forced if the Accept-Encoding header contains "gzip"
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
{
header('Content-Encoding: gzip');
ob_start('ob_gzhandler');
}
}
950Kb of HTML was compressed down around 80KB resulting in a 5-10x speed increasing loading the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With