如何检测是否有任何输出?如果有,如何重定向? [英] How can I detect if any output has been written? And how can I redirect if it has?
问题描述
理想情况下,我想直接回到错误页面使用标头('Location:...
,但是当然,如果有任何输出被写入,我不能这样做,所以...
1)如何检测是否有任何输出?
2)如果有新页面,我可以如何加载?
也许我应该缓冲整个页面,只有输出它,一旦我确定它是完整的,没有更多的错误可以发生。
使用PHP的内置输出缓冲。在最简单的情况下,您只需要调用 ob_start()
,然后才能产生输出。
是您可以利用的其他选项,如果您愿意的话:
http://www.php.net/manual/en/function.ob-start.php
Let's say that I want some generic error handling, which can be called from all over...
Ideally, I would like to just readirect to an error page using header('Location:...
but, of course I can't do that it if any output has been written, so ...
1) how can I detect if any output has been written?
2) how can I load a new page if it has?
Perhaps I should buffer the entire page and only output it once I am certain that it is complete and no more errors can occur?
Use PHP's built-in output buffering. At it's simplest, all you need to do is call ob_start()
before anything that would produce output.
There are other options you can take advantage of as well if you desire:
http://www.php.net/manual/en/function.ob-start.php
这篇关于如何检测是否有任何输出?如果有,如何重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!