如何检测是否有任何输出?如果有,如何重定向? [英] How can I detect if any output has been written? And how can I redirect if it has?

查看:197
本文介绍了如何检测是否有任何输出?如果有,如何重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想要一些通用的错误处理,可以从所有的调用...



理想情况下,我想直接回到错误页面使用标头('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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆