2013-03-30 55 views
0

我试图用register_shutdown_function来处理致命错误。我想显示错误页面时调用此函数,是这样的:php register_shutdown_function usage

`

register_shutdown_function('en795HandleShutdown'); 
... 
function en795HandleShutdown() { 
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; 
echo '<html xmlns="http://www.w3.org/1999/xhtml">'; 
echo '<head>'; 
echo ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'; 
echo ' <meta http-equiv="Content-Style-Type" content="text/css"/>'; 
echo ' <meta http-equiv="Content-Script-Type" content="text/javascript"/>'; 
echo ' <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />'; 
echo ' <title>SOMETITLE</title>'; 
echo '</head>'; 
echo '<body >'; 
echo '<div><div>'; 
echo '  <h1>Error</h1>'; 
echo '  <h2>Dead End.<h2>'; 
echo ' </div></div>'; 
echo '</body>'; 
echo '</html>'; 
} 

`

我产生通过设置ini_set('max_execution_time', 5);和执行不数据库来获取Fatal error: Maximum execution time...错误。但是在调用函数时什么都不会发生。我猜是什么问题。忘了提及:ini_set('display_errors', 0); TIA。

回答

0

如果您希望处理致命错误。你最好使用:

http://www.php.net/manual/en/function.set-error-handler.php

的联系,是set_error_handler();

function myErrorHandler($errno, $errstr, $errfile, $errline) 
{ 
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; 
echo '<html xmlns="http://www.w3.org/1999/xhtml">'; 
echo '<head>'; 
echo ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'; 
echo ' <meta http-equiv="Content-Style-Type" content="text/css"/>'; 
echo ' <meta http-equiv="Content-Script-Type" content="text/javascript"/>'; 
echo ' <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />'; 
echo ' <title>SOMETITLE</title>'; 
echo '</head>'; 
echo '<body >'; 
echo '<div><div>'; 
echo '  <h1>Error</h1>'; 
echo '  <h2>Dead End.<h2>'; 
echo ' </div></div>'; 
echo '</body>'; 
echo '</html>'; 


} 


set_error_handler('myErrorHandler'); 

register_shutdown_function();执行在最后,当你的脚本已经完成处理,所以一旦你的整个脚本已经完成。你将会看到你的'处理者'

0

贡献的答案是错误的问题。 set_error_handler回调不适用于致命错误。为此你必须使用register_shutdown_function。判断发生致命错误时是否调用的一种方法是调用error_get_last(http://php.net/manual/en/function.error-get-last.php),如果返回数组,则检查该类型是否为E_ERROR。