ob_get_level() starts at level 1
Asked Answered
J

1

7

Having a few problems with output buffering. Mainly, I'm trying to run output buffering with the ob_gzhandler callback, but it keeps telling me its using an unsupported compression type. Everything is enabled, and I believe the problem is that running ob_get_level() at the start of my script produces a level of 1. php.ini has my output_buffering set to 4096.

If I run something like:

while(ob_get_level() > 0){
   ob_end_clean();
}

Then I can successfully run ob_start() with the ob_gzhandler callback. But I'm wondering if it should be a problem. During my script I make calls to ob_clean() at various points as I'm avoiding stacking too many buffers as I've read this can increase performance. I'm just unsure as to what I should be doing here.

Cheers.

Jehol answered 4/9, 2010 at 9:13 Comment(0)
H
8

You have output buffering enabled by default (see the docs) - that basically means that every PHP script starts with ob_start().

If you want to disable the default OB for all PHP scripts, in your php.ini, set output_buffering = Off.

If you only want to disable the default OB for this specific script, use the while loop - it's quite correct.

As for the ob_clean - are you sure you want to delete the output that's in your buffer? IMO it's not really necessary, unless you are seeing significant slow page loads. Don't worry about optimizing that (at least not now).

Heti answered 4/9, 2010 at 9:19 Comment(4)
Yes, I believe I do. It's set as: output_buffering = 4096 (which I said above). What I'm getting at, is should I leave that as it is, and simply run the loop I have above so that I can start output buffering ob_gzhandler?Jehol
@Jason Lewis: That's up to you. I'd probably leave the default OB on for all scripts, and terminate it with the while loop in cases where I wanted to use my own.Heti
I was meant to say I use ob_get_clean() in a few places, to store output in a few variables and eventually it is flushed. Is this a good way of doing it, or should I start a new buffer for each place where I want to capture output and use ob_get_flush() instead?Jehol
It actually doesn't seem to be working when I run ob_start('ob_gzhandler') and then use ob_clean(). Seems to display the unsupported compression type. So I am using a master buffer (ob_start()), then capturing the required outputs throughout my application and cleaning the buffer afterwards. Then when it comes to display the final output I end the master buffer and start a new buffer with the compression, and it seems to be working.Jehol

© 2022 - 2024 — McMap. All rights reserved.