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

天涯浪子 提交于 2019-12-12 08:24:51

问题


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?


回答1:


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.




回答2:


if (ob_get_level())
    echo "ob already started";



回答3:


General:

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

More specific

$status = ob_get_status();
if ($status['name']=='ob_gzhandler') {
 // ob named ob_gzhandler started
}



回答4:


What about using it this way?

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



来源:https://stackoverflow.com/questions/6010403/how-to-determine-wether-ob-start-has-been-called-already

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!