在 PHP 中显示自定义错误页面以获取 set_error_handler 无法捕获的错误 [英] Displaying custom error page in PHP for errors which can't be caught by set_error_handler

查看:37
本文介绍了在 PHP 中显示自定义错误页面以获取 set_error_handler 无法捕获的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够丢弃部分呈现的页面并在 PHP 中显示错误页面.

I would like to be able to discard a partially rendered page and show an error page in PHP.

我已经知道set_error_handler(),但它只能捕获某些类型的错误.我想知道如何在 set_error_handler() 被引发.

I already know about set_error_handler(), but it can only trap certain types of errors. I would like to know how to show an error page when an error type which can't be trapped by set_error_handler() is raised.

不幸的是,当在 Apache 2.2 上使用 PHP 5.3.2 运行以下代码时,似乎没有达到我期望的效果:

Unfortunately, it seems that the following code, when run with PHP 5.3.2 on Apache 2.2, doesn't do what I would expect it to do:

<?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() 开启时输出错误信息并且只输出错误信息).

Whereas I would expect it to output nothing (or spew error info and only error info if display_errors() is on).

我已经确认使用 LiveHTTPHeaders 当 display_errors 关闭时 PHP 5.3.2 确实会向浏览器发送 500 错误(当它打开时为 200),使用 MacPorts 提供的 apache 版本,但它在使用时只会吐出 200 秒XAMPP 上的 PHP 5.3.1.

I have confirmed using LiveHTTPHeaders that PHP 5.3.2 does send a 500 error to the browser when display_errors is off (and a 200 when it's on) using the version of apache supplied by MacPorts, but it only ever spits 200s when using PHP 5.3.1 on XAMPP.

我尝试在 apache 配置中设置 ErrorDocument 500 "test"(通过对 404 执行相同操作,确认可以正常工作)但 PHP 从不显示自定义错误,即使脚本的全部内容只是 header('HTTP/1.1 500 内部服务器错误');

I tried setting ErrorDocument 500 "test" in the apache configuration (confirmed to be working by doing the same for 404) but PHP never shows the custom error, even when the entire contents of the script is just header('HTTP/1.1 500 Internal Server Error');

我不知道还能做些什么来确保部分呈现的页面被一个简单的错误所取代.

I'm not sure what else to do to make sure a partially rendered page is replaced with a simple error.

我也可以确认这发生在 Yii 框架中.如果我编辑博客演示中关于"页面的视图,使其有一行内容为 <?php echo function_which_does_not_exist() ?>,我会得到一个部分呈现的页面.

I can also confirm that this happens in the Yii framework. If I edit the view for the "about" page in the blog demo to have a line which reads <?php echo function_which_does_not_exist() ?>, I get a partially rendered page.

推荐答案

您可以传递 ob_start 回调函数的名称,该函数在 ob_get_clean( 上刷新输出) 之前执行.

You could pass ob_start the name of a callback function, that is executed before the output is flushed on ob_get_clean().

即使页面发生错误,这个回调函数也会被执行.

This callback function seams to be executed even if an error occured on the page.

这样你就可以做这样的事情:

This way you could do something like this:

<?php
$endReached = 0;

function outpu_cb($buffer) {
  global $endReached;

  if ($endReached) return $buffer;
  else return 'Your error message';
}

// Start the output buffer
ob_start('outpu_cb');

// 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.
$endReached = 1;
echo ob_get_clean();
?>

这篇关于在 PHP 中显示自定义错误页面以获取 set_error_handler 无法捕获的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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