在PHP中,是否有一种(现成的)方法可以检查输出的一部分是否已经发送到客户端?
我知道使用headers_sent()
,您可以检查标头是否已经发送,但我也想检查是否发送了任何输出(例如HTTP标头Content-Length
>0)。
(请注意,ob_start()
从被调用的那一刻起就开始输出缓冲。但实现我的代码的第三方系统可能已经发送了输出,因此据我所知,输出缓冲不可用。)
您可以将自己的ob_start包装在其他所有东西上,并在决定时刷新它。即使其他ob_start和刷新都在里面,它也能工作。举个例子:
ob_start();
[...]
//some 3rd party app which is included on the way
[...]
ob_start();
[...]
ob_flush(); //at this moment the buffer isn't flushed to the client
//but to the parent ob_start, which is yours, so no output
//is sent yet
[...]
//ok, we're done, we can output now
ob_flush();
PHP在大多数情况下只向Apache发送带有某些内容的头,或者在脚本执行结束时发送头。因此,如果发送了标头,也会发送一些内容。此外,据我所知,Content-Length
无论如何都是在脚本执行之后计算的。