PHP ob_start() and ob_start('ob_gzhandler')
Asked Answered
D

2

12

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

Doone answered 16/5, 2012 at 9:4 Comment(0)
S
16

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.

Shuck answered 16/5, 2012 at 9:8 Comment(2)
If a page uses ob_start('ob_gzhandler') which speeds up the content transfer to the client, I think this buffering may speed up that page rendering as well.Doone
Those two things is not related to eachother. Page rendering depends on the clients hardware. It doesn't matter if I can have the content in 1 second if rendering the content takes 3 seconds. You still have to fetch the content before you can render anything.Shuck
V
6

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.

Varia answered 5/2, 2016 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.