Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine wether ob_start(); has been called already

I use output buffering for gzip compression and access to what was put out before in a PHP script:

if(!ob_start("ob_gzhandler")) ob_start();

Now if that script gets included in another script where ob_start() already is in use I get a warning:

Warning: ob_start() [ref.outcontrol]: output handler 'ob_gzhandler' cannot be used twice in filename on line n

So I'd like to test wether ob_start() has already been called. I think ob_get_status() should be what I need but what is the best way to use it in testing for this?

like image 828
C.O. Avatar asked May 15 '11 18:05

C.O.


People also ask

What does Ob_start () mean?

Definition and Usage 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().

Should I use Ob_start?

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). Save this answer.


1 Answers

ob_get_level returns the number of active output control handlers and ob_list_handlers returns a lift of those handlers. So you could do this:

if (!in_array('ob_gzhandler', ob_list_handlers())) {
    ob_start('ob_gzhandler');
} else {
    ob_start();
}

Although in general you can call ob_start any number of times you want, using ob_gzhandler as handler cannot as you would compress already compressed data.

like image 166
Gumbo Avatar answered Oct 20 '22 09:10

Gumbo