【发布时间】:2010-07-15 06:46:32
【问题描述】:
我希望能够丢弃部分呈现的页面并在 PHP 中显示错误页面。
我已经知道set_error_handler(),但它只能捕获某些类型的错误。我想知道当出现set_error_handler() 无法捕获的错误类型时如何显示错误页面。
不幸的是,以下代码在 Apache 2.2 上使用 PHP 5.3.2 运行时,似乎并没有达到我的预期:
<?php
// Start the output buffer
ob_start();
// Output something into the buffer.
// I only want this to be displayed if I call one of the
// ob_flush functions or echo the buffer myself later.
echo "yep";
// Call a function I know not to exist in order to raise
// an error which cannot be trapped by set_error_handler()
// and would, if display_errors was On, output "Fatal
// error: Call to undefined function fwee()..."
function_which_does_not_exist();
// This will never be executed.
$out = ob_get_clean();
脚本的输出是:
yep
而我希望它不输出任何内容(或者如果 display_errors() 开启,则只输出错误信息和错误信息)。
我已经使用 LiveHTTPHeaders 确认 PHP 5.3.2 使用 MacPorts 提供的 apache 版本在 display_errors 关闭时会向浏览器发送 500 错误(打开时会发送 200),但在使用时它只会吐出 200 秒XAMPP 上的 PHP 5.3.1。
我尝试在 apache 配置中设置 ErrorDocument 500 “test”(通过对 404 执行相同操作确认可以正常工作),但 PHP 从不显示自定义错误,即使脚本的全部内容只是 header('HTTP/1.1 500 Internal Server Error');
我不确定还有什么方法可以确保部分呈现的页面被替换为一个简单的错误。
我也可以确认这发生在 Yii 框架中。如果我在博客演示中编辑“关于”页面的视图以显示<?php echo function_which_does_not_exist() ?> 的行,我会得到一个部分呈现的页面。
【问题讨论】: