How to determine whether ob_start(); has been called already
Asked Answered
C

4

15

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 whether 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?

Combe answered 15/5, 2011 at 18:40 Comment(0)
C
18

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.

Cychosz answered 15/5, 2011 at 18:47 Comment(2)
I was irritated by the comment on the manual that claims "Sometimes, ob_get_level() may be off by 1 because at the start of the script, it will return 1 even if ob_start() has never been called". So is this a reliable way to tell?Combe
@C.O.: That is probably be due to output_buffering: “If output_buffering is enabled or an anonymous function was used with ob_start(), ob_list_handlers() will return "default output handler".”Cychosz
C
11
if (ob_get_level())
    echo "ob already started";
Chema answered 15/5, 2011 at 18:43 Comment(4)
I was irritated by the comment on the manual that claims "Sometimes, ob_get_level() may be off by 1 because at the start of the script, it will return 1 even if ob_start() has never been called". So is this a reliable way to tell?Combe
@Combe I've read the comment, but this is not true. It will return 0 even if called at the start of the script. You could have a look at my example: codepad.org/SrZ4YdQnChema
It is true. Ran into it last week on a shared server. Best way to handle it is to start your code with if(ob_get_level())ob_end_clean(); before ob_start()Theravada
@Theravada That is probably due to server starting output buffering automatically.Chema
C
6

General:

if (ob_get_status())  {
  // ob started
}

More specific

$status = ob_get_status();
if ($status['name']=='ob_gzhandler') {
 // ob named ob_gzhandler started
}
Concentrate answered 15/5, 2011 at 18:45 Comment(1)
That was what I was looking for. Thanks. I'll use Gumbo's suggestion above I think.Combe
P
2

What about using it this way?

if (ob_get_level() == 0) ob_start();

Peavey answered 15/5, 2011 at 18:43 Comment(3)
I was irritated by the comment on the manual that claims "Sometimes, ob_get_level() may be off by 1 because at the start of the script, it will return 1 even if ob_start() has never been called". So is this a reliable way to tell?Combe
At least there is no bug report for this kind of problem. Might be the commenters fault. bugs.php.net/…Peavey
Yes probably but that comment was what led me to start this question. I wanted to get some opinions. So thanks.Combe

© 2022 - 2024 — McMap. All rights reserved.