Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ob_start() and ob_start('ob_gzhandler')

What is the difference between using ob_start() and ob_start('ob_gzhandler') ?
How does it affect the page speed ?

like image 835
Sithu Avatar asked May 16 '12 09:05

Sithu


People also ask

What is the use of Ob_start () in PHP?

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.

What is Ob_start () and Ob_end_flush () in PHP?

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().

What is Ob_start Wordpress?

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).

What is ob_ gzhandler?

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.


2 Answers

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.

like image 75
Repox Avatar answered Sep 22 '22 13:09

Repox


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.

like image 31
Nick Bedford Avatar answered Sep 21 '22 13:09

Nick Bedford