我可以使用set_error_handler()
来捕获大多数PHP错误,但它无法处理致命错误(E_ERROR
),例如调用不存在的函数。是否有另一种方法来捕获这些错误?
我正尝试在所有错误时调用mail()
,并且正在运行PHP 5.2.3。
我可以使用set_error_handler()
来捕获大多数PHP错误,但它无法处理致命错误(E_ERROR
),例如调用不存在的函数。是否有另一种方法来捕获这些错误?
我正尝试在所有错误时调用mail()
,并且正在运行PHP 5.2.3。
使用register_shutdown_function
记录致命错误,需要PHP 5.2+:
register_shutdown_function( "fatal_handler" );
function fatal_handler() {
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if($error !== NULL) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
error_mail(format_error( $errno, $errstr, $errfile, $errline));
}
}
您需要定义error_mail
和format_error
函数。例如:
function format_error( $errno, $errstr, $errfile, $errline ) {
$trace = print_r( debug_backtrace( false ), true );
$content = "
<table>
<thead><th>Item</th><th>Description</th></thead>
<tbody>
<tr>
<th>Error</th>
<td><pre>$errstr</pre></td>
</tr>
<tr>
<th>Errno</th>
<td><pre>$errno</pre></td>
</tr>
<tr>
<th>File</th>
<td>$errfile</td>
</tr>
<tr>
<th>Line</th>
<td>$errline</td>
</tr>
<tr>
<th>Trace</th>
<td><pre>$trace</pre></td>
</tr>
</tbody>
</table>";
return $content;
}
使用Swift Mailer编写error_mail
函数。mail("myname@myemail.com", "My Site: FATAL ERROR", "Details: " . $errno . ' ' . $errstr . ' ' . $errfile . ' ' . $errline);
这行代码会发送一封邮件到"myname@myemail.com",邮件标题为"My Site: FATAL ERROR",内容包括错误相关的详细信息。其中,"$errno"代表错误号码,"$errstr"代表错误信息,"$errfile"代表发生错误的文件名,"$errline"代表发生错误的行数。 - Eric我刚想出了这个解决方案(PHP 5.2.0+):
function shutDownFunction() {
$error = error_get_last();
// Fatal error, E_ERROR === 1
if ($error['type'] === E_ERROR) {
// Do your stuff
}
}
register_shutdown_function('shutDownFunction');
不同的错误类型在预定义常量中有定义。
register_shutdown_function()
必须早于任何致命错误。在use_1T_memory(); /* memory exhausted error here! */ register_shutdown_function('shutDownFunction');
中使用该函数将无法按预期工作。 - NobuPHP没有提供传统的方法来捕获和恢复致命错误。这是因为通常在致命错误后处理过程不应该被恢复。使用字符串匹配输出缓冲区(如PHP.net上所述的技术)绝对是不明智的。这种方法根本不可靠。
在错误处理程序方法中调用mail()函数也会带来问题。如果有很多错误,你的邮件服务器将会负载过重,你可能会发现自己的收件箱里满是麻烦。为避免这种情况,你可以考虑定期运行cron扫描错误日志并相应地发送通知。你还可以看看系统监控软件,例如Nagios。
关于注册关闭函数:
确实可以注册一个关闭函数,这是个好答案。
这里的重点是我们通常不应该尝试从致命错误中恢复,特别是不应该通过针对输出缓冲区进行正则表达式匹配。我在回应接受的答案,该答案链接到了一个建议在php.net上的内容,但已经被更改或删除了。
该建议是在异常处理期间针对输出缓冲区使用正则表达式,在检测到致命错误(通过匹配你可能期望的任何配置错误文本)时,尝试进行一些恢复或继续处理。这不是推荐的做法(我认为这也是为什么我找不到原始建议的原因之一,我可能正在忽略它,或php社区已经否定了它)。
值得注意的是,PHP的更新版本(大约5.1左右)似乎会在调用输出缓冲回调函数之前更早地调用关闭函数。在5及更早版本中,这种顺序是相反的(输出缓冲回调函数后面是关闭函数)。此外,自约5.0.5以来(远早于提问者的版本5.2.3),对象在注册的关闭函数被调用之前就已经被卸载了,因此您不能依赖内存中的对象来完成任何任务。在PHP 7及更高版本中,致命错误或可恢复的致命错误现在会抛出Error
实例。与其他异常一样,可以使用try/catch
块捕获Error
对象。
示例:
<?php
$variable = 'not an object';
try {
$variable->method(); // Throws an Error object in PHP 7 or higger.
} catch (Error $e) {
// Handle error
echo $e->getMessage(); // Call to a member function method() on string
}
或者您可以使用Throwable
接口来捕获所有异常。
示例:
<?php
try {
undefinedFunctionCall();
} catch (Throwable $e) {
// Handle error
echo $e->getMessage(); // Call to undefined function undefinedFunctionCall()
}
ReflectionClass
时出现的类似于“致命错误:Trait 'FailedTrait'未找到”的错误? - TCB13include "filename.php"
代替,这样至少可以让 Throwable
catch 块能够捕获 ParseError
。 - Niloct嗯,似乎有其他方法可以捕获致命错误 :)
ob_start('fatal_error_handler');
function fatal_error_handler($buffer){
$error = error_get_last();
if($error['type'] == 1){
// Type, message, file, line
$newBuffer='<html><header><title>Fatal Error </title></header>
<style>
.error_content{
background: ghostwhite;
vertical-align: middle;
margin:0 auto;
padding: 10px;
width: 50%;
}
.error_content label{color: red;font-family: Georgia;font-size: 16pt;font-style: italic;}
.error_content ul li{ background: none repeat scroll 0 0 FloralWhite;
border: 1px solid AliceBlue;
display: block;
font-family: monospace;
padding: 2%;
text-align: left;
}
</style>
<body style="text-align: center;">
<div class="error_content">
<label >Fatal Error </label>
<ul>
<li><b>Line</b> ' . $error['line'] . '</li>
<li><b>Message</b> ' . $error['message'] . '</li>
<li><b>File</b> ' . $error['file'] . '</li>
</ul>
<a href="javascript:history.back()"> Back </a>
</div>
</body></html>';
return $newBuffer;
}
return $buffer;
}
你无法捕获/处理致命错误,但可以将它们记录/报告。
为了快速调试,我修改了这段简单代码中的一个答案
function __fatalHandler()
{
$error = error_get_last();
// Check if it's a core/fatal error, otherwise it's a normal shutdown
if ($error !== NULL && in_array($error['type'],
array(E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING,
E_COMPILE_ERROR, E_COMPILE_WARNING,E_RECOVERABLE_ERROR))) {
echo "<pre>fatal error:\n";
print_r($error);
echo "</pre>";
die;
}
}
register_shutdown_function('__fatalHandler');
我开发了一种方法来捕获 PHP 中的所有错误类型(几乎所有)!我不确定 E_CORE_ERROR 是否有效(我认为仅对该错误无效)!但是,对于其他致命错误(E_ERROR、E_PARSE、E_COMPILE 等),只使用一个错误处理函数即可正常工作!以下是我的解决方案:
将下面的代码放在您的主文件(index.php)中:
<?php
define('E_FATAL', E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR |
E_COMPILE_ERROR | E_RECOVERABLE_ERROR);
define('ENV', 'dev');
// Custom error handling vars
define('DISPLAY_ERRORS', TRUE);
define('ERROR_REPORTING', E_ALL | E_STRICT);
define('LOG_ERRORS', TRUE);
register_shutdown_function('shut');
set_error_handler('handler');
// Function to catch no user error handler function errors...
function shut(){
$error = error_get_last();
if($error && ($error['type'] & E_FATAL)){
handler($error['type'], $error['message'], $error['file'], $error['line']);
}
}
function handler( $errno, $errstr, $errfile, $errline ) {
switch ($errno){
case E_ERROR: // 1 //
$typestr = 'E_ERROR'; break;
case E_WARNING: // 2 //
$typestr = 'E_WARNING'; break;
case E_PARSE: // 4 //
$typestr = 'E_PARSE'; break;
case E_NOTICE: // 8 //
$typestr = 'E_NOTICE'; break;
case E_CORE_ERROR: // 16 //
$typestr = 'E_CORE_ERROR'; break;
case E_CORE_WARNING: // 32 //
$typestr = 'E_CORE_WARNING'; break;
case E_COMPILE_ERROR: // 64 //
$typestr = 'E_COMPILE_ERROR'; break;
case E_CORE_WARNING: // 128 //
$typestr = 'E_COMPILE_WARNING'; break;
case E_USER_ERROR: // 256 //
$typestr = 'E_USER_ERROR'; break;
case E_USER_WARNING: // 512 //
$typestr = 'E_USER_WARNING'; break;
case E_USER_NOTICE: // 1024 //
$typestr = 'E_USER_NOTICE'; break;
case E_STRICT: // 2048 //
$typestr = 'E_STRICT'; break;
case E_RECOVERABLE_ERROR: // 4096 //
$typestr = 'E_RECOVERABLE_ERROR'; break;
case E_DEPRECATED: // 8192 //
$typestr = 'E_DEPRECATED'; break;
case E_USER_DEPRECATED: // 16384 //
$typestr = 'E_USER_DEPRECATED'; break;
}
$message =
'<b>' . $typestr .
': </b>' . $errstr .
' in <b>' . $errfile .
'</b> on line <b>' . $errline .
'</b><br/>';
if(($errno & E_FATAL) && ENV === 'production'){
header('Location: 500.html');
header('Status: 500 Internal Server Error');
}
if(!($errno & ERROR_REPORTING))
return;
if(DISPLAY_ERRORS)
printf('%s', $message);
//Logging error on php file error log...
if(LOG_ERRORS)
error_log(strip_tags($message), 0);
}
ob_start();
@include 'content.php';
ob_end_flush();
?>
您不能在已注册的关闭函数中这样抛出异常:
<?php
function shutdown() {
if (($error = error_get_last())) {
ob_clean();
throw new Exception("fatal error");
}
}
try {
$x = null;
$x->method()
} catch(Exception $e) {
# This won't work
}
?>
<?php
function shutdown() {
if (($error = error_get_last())) {
ob_clean();
# Report the event, send email, etc.
header("Location: http://localhost/error-capture");
# From /error-capture. You can use another
# redirect, to e.g. the home page
}
}
register_shutdown_function('shutdown');
$x = null;
$x->method()
?>
如果您使用的是PHP >= 5.1.0,只需像这样使用ErrorException类:
<?php
// Define an error handler
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
// Set your error handler
set_error_handler("exception_error_handler");
/* Trigger exception */
try
{
// Try to do something like finding the end of the internet
}
catch(ErrorException $e)
{
// Anything you want to do with $e
}
?>
我需要处理生产环境中的致命错误,并显示一个静态样式的 503服务不可用 HTML 输出。这肯定是一种合理的“捕获致命错误”的方法。以下是我的做法:
我有一个自定义的错误处理函数“error_handler”,它会在任何 E_ERROR,E_USER_ERROR 等错误时显示我的“503服务不可用”HTML页面。现在,在关闭函数上调用该函数,以捕获我的致命错误。
function fatal_error_handler() {
if (@is_array($e = @error_get_last())) {
$code = isset($e['type']) ? $e['type'] : 0;
$msg = isset($e['message']) ? $e['message'] : '';
$file = isset($e['file']) ? $e['file'] : '';
$line = isset($e['line']) ? $e['line'] : '';
if ($code>0)
error_handler($code, $msg, $file, $line);
}
}
set_error_handler("error_handler");
register_shutdown_function('fatal_error_handler');
在我的自定义error_handler函数中,如果错误是E_ERROR、E_USER_ERROR等,我也会调用@ob_end_clean();
来清空缓冲区,从而删除PHP的“致命错误”信息。@
静音函数,因为我们不希望我们的error_handler脚本生成任何错误。