开发者

Detect if output already sent in php

I'm sending content to the browser via gzip if they support it (for css/js), currently I just use :

stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== False

However, if there was some output before then (ie. php error/warning), this breaks the gzip output and the browser (firefox at least) says the site is usi开发者_Python百科ng an unsupported form of compression (because of the the prior output).

So, is there a way to detect if output was sent? or is there a better way to do it?


You can use headers_sent() i believe.


PHP provides the headers_sent() function to serve this purpose.


To complement the response of @prodigitalson, which is right, you can us the ob_* functions, ob_start, ob_start, etc. This will handle an Output Buffer (ob), and all the sent data is not really sent. An any time after the ob_start you can decide to flush (ob_end_flush) the buffer and it will be really sent, but you can as well use ob_end_clean to empty the buffer content. Check the documentation.

Output buffering can speed up outgoing communication of the response, and let you erase all the thing you've already responded, which is always a problem in PHP. Nice tool.


you can use

ob_start() ob_flush()

to capture any output generated and either output it as a proper error message or ignore it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜