如果您是php老手,当然知道当php脚本出错时发生了什么事情。此时php解析器将在屏幕上给出错误信息,如 fatal error: call to undefined function on line 19 --,因此程序在此处终止。这个信息会吓到客户,他可能立即打电话和你进行咨询。
幸运的是,这里有解决办法。php拥有内置工具,可以让开发人员捕捉脚本错误然后将它们转到自定义的错误处理器。此时则可以对处理器进行编程显示更多关于错误的详细信息。还可以将错误写入文件或数据库以采取补救措施。有时候还可以对处理器编写程序忽略错误消息。
本文中,我将阐述如何使用php的错误处理api构建用户自定义的错误处理器,并且说明如何以简单而友好的方式显示和管理脚本的错误信息。
错误类型和报告级别
我们从最基本的开始。php有三种最基本的错误类型,从低级到高级分别为:注意、警告和错误(或致命错误)。通常情况下,注意和警告不会终止程序;但是致命错误则是危险故障(例如,调用一个没有定义的函数或参考一个不存在的对象),将导致程序中断。这些错误有可能在启动、解析、编译或运行时发生。
关键字如e_notice, e_error等用于表明错误的不同类型和等级。在php手册上可以获得它们的详细信息列表。
脚本阶段错误显示由error_reporting()函数进行控制。这一函数针对不同的错误等级设置不同的参数。表a给出了使用此函数报告警告和致命错误的脚本程序。
表a
<?php
// display warnings and errors
error_reporting(e_warning | e_error);
// this will generate a notice, which will never be displayed
echo $undefinedvar;
// this will generate a fatal error, which will be displayed
callundeffunc();
?>
将表b中的代码与上面的进行比较发现,listing b中隐藏错误信息甚至隐藏致命信息,使得错误信息不会被显示出来。
表b
<?php
// turn off error display
// no errors will be displayed
error_reporting(0);
// this will generate a notice
echo $undefinedvar;
// this will generate a fatal error
callundeffunc();
?>
表c中的代码将所有错误信息甚至简单的注意事项都显示出来:
表c
<?php
// all errors will be displayed
error_reporting(e_all);
// this will generate a notice
echo $undefinedvar;
// this will generate a fatal error
callundeffunc();
?>
如以上3个例子所示,error_reporting()函数在控制错误发生时,在屏幕上显示内容非常重要。这里的关键字是displayed,其表达的意思是错误不被显示而不是错误没有发生。因此,发生致命错误时(例如不正确的函数调用),程序将被终止;但是,此时没有任何消息显示给用户。
下面的例子(表 d)说明了这种情况:
表d
<?php
// no errors will be displayed
error_reporting(0);
// start a task
echo "starting task...";
// call an undefined function
// a fatal error occurs during task processing
callme();
// end the task
echo "successfully completed task...";
?>
在表d中,在调用echo()函数时发生了致命错误,程序执行时到这点被终止,但是却没有任何错误消息给出,用户不知道这种情况还以为程序在正确运行。下面的结论是非常明显的:不给出错误报告非常危险,因为不论过程是否完成它常导致不正确的结论。
注意:调用不带任何参数的error_reporting()将返回当前的错误报告等级。
自定义错误处理器
很明显,隐藏错误报告是不正确的,你肯定想知道有什么其他办法加以改进。自定义错误处理器就是一个很好的能取代php默认错误处理系统的解决方法。自定义错误处理器可以以任何方式设置处理错误信息,包括信息如何显示。
php函数中,完成这一功能的是set_error_handler()函数。错误发生时,此函数被自动调用,然后给出4个参数:错误代码、错误消息、发生错误的脚本名称和错误出现的行,此函数对错误管理负责。
表e给出一个简单例子:
表e
<?php
// define custom handler
set_error_handler('myhandler');
// custom handler code
function myhandler($code, $msg, $file, $line) {
echo "just so you know, something went wrong at line $line of your script $file. the system says that the error code was $code, and the reason for the error was: $msg. sorry about this!";
}
// generate a notice
echo $undefvar;
?>
当运行此脚本的时候,会出现下面的信息:
just so you know, something went wrong at line 11 of your /dev/error1.php. the system says that the error code was 8, and the reason for the error was: undefined variable: undefvar. sorry about this!
此时,php的默认错误处理器被用户定义的myhandler()函数所取代,$undefvar变量被激活,php通知未定义变量的信息,此信息在运行时引擎产生,然后传递给myhandler()函数,同时错误发生的地址也传递给此函数。然后myhandler()函数输出友好信息解释错误。
注意:错误和致命错误很重要,它们会绕过自定义错误处理器,然后以php默认的错误处理机制进行显示。显示这些信息可使用前面讨论的标准error_reporting()函数进行控制。
例1:动态错误页面和e-mail警报
表f给出了另一个范例,当发生错误时,将动态产生html错误页面,并且通过e-mail向web管理员进行报告。
表f
<?php
// define custom handler
set_error_handler('myhandler');
// custom handler code
function myhandler($code, $msg, $file, $line, $context) {
// print error page
echo "<html><head></head><body>";
echo "<h2 align=center>error!</h2>";
echo "<font color=red size=+1>";
echo "an error occurred while processing your request. please visit our <a href=http://www.domain.dom>home page</a> and try again.";
echo "</font>";
echo "</body></html>";
// email error to admin
$body = "$msg at $file ($line), timed at " . date ("d-m-y h:i:s", mktime());
$body .= "/n/n" . print_r($context, true);
mail ("[email protected]", "web site error", $body);
// halt execution of script
die();
}
// generate a notice
echo $undefvar;
?>
这里,自定义的错误处理器在遇到错误时动态产生html错误页面。此错误信息也能被e-mail信息捕获,然后通过php内置的mail()函数发送给管理员。
这里出现了myhandler()函数的一个新参数$context。这是myhandler()函数的第五个参数,是可选项。它包含了当前变量状态的快照。包括对管理员有用的上下文信息,有利于减少调试时间。
例2:自定义错误日志
表g给出了另一个例子,这个例子说明自定义错误处理器如何将详细的错误信息输入到文件。
表g
<?php
// define custom handler
set_error_handler('myhandler');
// custom handler code
function myhandler($code, $msg, $file, $line) {
// print error page
echo "<html><head></head><body>";
echo "<h2 align=center>error!</h2>";
echo "<font color=red size=+1>";
echo "an error occurred while processing your request. please visit our <a href=http://www.domain.dom>home page</a> and try again.";
echo "</font>";
echo "</body></html>";
// log error to file, with context
$logdata = date("d-m-y h:i:s", mktime()) . ", $code, $msg, $line, $file/n";
file_put_contents("web.log", $logdata, file_append);
// halt execution of script
die();
}
// generate a warning
echo is_float();
?>
与前面的例子相似,它也产生一个错误页面并且将错误数据输入到文件,以利于管理员进行查看。数据以csv格式进行存储,并且有简单的数据分析和报告。请注意在本例和前面实例中,错误处理代码结束时调用die()函数,以确保脚本不再运行。
如上面的范例所示,自定义错误处理器允许以友好的方式处理php脚本错误。并且可以发挥自己的创造性,不过需要记住的是:任何灵活性的增加都伴随着开销和时间的增加。
新闻热点
疑难解答