如何确定更好的ob_start();已被称为

时间:2021-07-08 00:40:45

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

我使用输出缓冲进行gzip压缩并访问以前在PHP脚本中输出的内容:

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:

现在,如果该脚本包含在另一个已经使用ob_start()的脚本中,我会收到警告:

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

警告:ob_start()[ref.outcontrol]:输出处理程序'ob_gzhandler'不能在第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?

所以我想测试一下ob_start()已经被调用了。我认为ob_get_status()应该是我需要的,但是在测试中使用它的最佳方法是什么?

4 个解决方案

#1


14  

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:

ob_get_level返回活动输出控件处理程序的数量,ob_list_handlers返回这些处理程序的提升。所以你可以这样做:

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.

虽然一般情况下你可以调用ob_start任意多次,但使用ob_gzhandler作为处理程序不能像压缩已经压缩的数据那样。

#2


10  

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

#3


4  

General:

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

More specific

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

#4


3  

What about using it this way?

用这种方式怎么样?

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

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

#1


14  

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:

ob_get_level返回活动输出控件处理程序的数量,ob_list_handlers返回这些处理程序的提升。所以你可以这样做:

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.

虽然一般情况下你可以调用ob_start任意多次,但使用ob_gzhandler作为处理程序不能像压缩已经压缩的数据那样。

#2


10  

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

#3


4  

General:

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

More specific

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

#4


3  

What about using it this way?

用这种方式怎么样?

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

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