too*_*php 536 php fatal-error
我可以set_error_handler()用来捕获大多数PHP错误,但它不适用于致命(E_ERROR)错误,例如调用不存在的函数.还有另一种方法来捕捉这些错误吗?
我试图调用mail()所有错误,并运行PHP 5.2.3.
小智 622
使用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));
}
}
Run Code Online (Sandbox Code Playgroud)
您必须定义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;
}
Run Code Online (Sandbox Code Playgroud)
使用Swift Mailer编写error_mail函数.
也可以看看:
per*_*lis 142
刚刚提出这个解决方案(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');
Run Code Online (Sandbox Code Playgroud)
在http://www.php.net/manual/en/errorfunc.constants.php中定义了不同的错误类型
kep*_*aro 114
PHP不提供用于捕获和从致命错误中恢复的常规方法.这是因为在致命错误之后通常不应该恢复处理.匹配输出缓冲区的字符串(正如PHP.net上描述的原始帖子所建议的那样)绝对是不明智的.这简直不可靠.
从错误处理程序方法中调用mail()函数也证明是有问题的.如果你有很多错误,你的邮件服务器将被加载工作,你可能会发现自己有一个粗糙的收件箱.为避免这种情况,您可以考虑运行cron来定期扫描错误日志并相应地发送通知.您可能还想查看系统监控软件,例如Nagios.
要谈谈注册关机功能:
你可以注册关机功能,这是一个很好的答案.
这里的要点是我们通常不应该尝试从致命错误中恢复,特别是不要通过对输出缓冲区使用正则表达式.我正在回答接受的答案,该答案与php.net上的一个建议有关,后者已被更改或删除.
该建议是在异常处理期间对输出缓冲区使用正则表达式,并且在致命错误的情况下(通过匹配您可能期望的任何已配置的错误文本来检测),尝试进行某种恢复或继续处理.这不是一个推荐的做法(我相信这也是我无法找到原始建议的原因.我要么忽略它,要么php社区将其击落).
值得注意的是,在输出缓冲回调被调用之前,更新版本的PHP(大约5.1)似乎更早地调用了shutdown函数.在版本5和更早版本中,该顺序是相反的(输出缓冲回调后跟关闭函数).此外,由于大约5.0.5(比提问者的版本5.2.3早得多),在调用注册的关闭函数之前很快就会卸载对象,因此您将无法依赖内存中的对象来执行操作很多东西.
因此,注册关闭功能很好,但是应该由关闭功能执行的任务可能仅限于少数温和的关闭过程.
对于那些偶然发现这个问题并在最初接受的答案中看到建议的人来说,这里的关键外卖只是一些智慧的话语.不要正则表达你的输出缓冲区.
sak*_*zai 38
那么似乎可以通过其他方式捕获致命错误:)
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;
}
Run Code Online (Sandbox Code Playgroud)
Lug*_*aue 24
致命错误或者可恢复的致命错误,现在抛出的情况下Error在PHP 7或更高版本.与任何其他异常一样,Error可以使用try/catch块捕获对象.
例:
<?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
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用Throwableinterface来捕获所有异常.
例:
<?php
try {
undefinedFunctionCall();
} catch (Throwable $e) {
// Handle error
echo $e->getMessage(); // Call to undefined function undefinedFunctionCall()
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问:http://php.net/manual/en/language.errors.php7.php
Luc*_*ssi 23
我开发了一种方法来捕获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();
?>
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助很多人!我在寻找这个解决方案的时间太长了,没找到!然后我开发了一个!
zai*_*eer 18
您无法捕获/处理致命错误,但您可以记录/报告它们.为了快速调试,我修改了这个简单代码的一个答案
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');
Run Code Online (Sandbox Code Playgroud)
hip*_*ker 17
您不能在注册的关闭函数中抛出异常,如下所示:
<?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
}
?>
Run Code Online (Sandbox Code Playgroud)
但您可以捕获请求并将请求重定向到另一个页面.
<?php
function shutdown() {
if (($error = error_get_last())) {
ob_clean();
# raport the event, send email etc.
header("Location: http://localhost/error-capture");
# from /error-capture, you can use another redirect, to e.g. home page
}
}
register_shutdown_function('shutdown');
$x = null;
$x->method()
?>
Run Code Online (Sandbox Code Playgroud)
小智 11
如果您使用的是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 ur error handle
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
}
?>
Run Code Online (Sandbox Code Playgroud)
/**
* ErrorHandler that can be used to catch internal PHP errors
* and convert to an ErrorException instance.
*/
abstract class ErrorHandler
{
/**
* Active stack
*
* @var array
*/
protected static $stack = array();
/**
* Check if this error handler is active
*
* @return bool
*/
public static function started()
{
return (bool) static::getNestedLevel();
}
/**
* Get the current nested level
*
* @return int
*/
public static function getNestedLevel()
{
return count(static::$stack);
}
/**
* Starting the error handler
*
* @param int $errorLevel
*/
public static function start($errorLevel = \E_WARNING)
{
if (!static::$stack) {
set_error_handler(array(get_called_class(), 'addError'), $errorLevel);
}
static::$stack[] = null;
}
/**
* Stopping the error handler
*
* @param bool $throw Throw the ErrorException if any
* @return null|ErrorException
* @throws ErrorException If an error has been catched and $throw is true
*/
public static function stop($throw = false)
{
$errorException = null;
if (static::$stack) {
$errorException = array_pop(static::$stack);
if (!static::$stack) {
restore_error_handler();
}
if ($errorException && $throw) {
throw $errorException;
}
}
return $errorException;
}
/**
* Stop all active handler
*
* @return void
*/
public static function clean()
{
if (static::$stack) {
restore_error_handler();
}
static::$stack = array();
}
/**
* Add an error to the stack
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
*/
public static function addError($errno, $errstr = '', $errfile = '', $errline = 0)
{
$stack = & static::$stack[count(static::$stack) - 1];
$stack = new ErrorException($errstr, 0, $errno, $errfile, $errline, $stack);
}
}
Run Code Online (Sandbox Code Playgroud)
ErrorHandler如果需要,此类允许您有时启动特定类.然后你也可以停止处理程序.
使用此类,例如:
ErrorHandler::start(E_WARNING);
$return = call_function_raises_E_WARNING();
if ($innerException = ErrorHandler::stop()) {
throw new Exception('Special Exception Text', 0, $innerException);
}
// or
ErrorHandler::stop(true); // directly throws an Exception;
Run Code Online (Sandbox Code Playgroud)
链接到完整的类代码:https:
//github.com/zendframework/zf2/blob/master/library/Zend/Stdlib/ErrorHandler.php
它还可以使用该register_shutdown_function函数处理FATAL_ERRORS .根据这个类,FATAL_ERROR是以下之一array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR).
class ErrorHandler
{
// [...]
public function registerExceptionHandler($level = null, $callPrevious = true)
{
$prev = set_exception_handler(array($this, 'handleException'));
$this->uncaughtExceptionLevel = $level;
if ($callPrevious && $prev) {
$this->previousExceptionHandler = $prev;
}
}
public function registerErrorHandler(array $levelMap = array(), $callPrevious = true, $errorTypes = -1)
{
$prev = set_error_handler(array($this, 'handleError'), $errorTypes);
$this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
if ($callPrevious) {
$this->previousErrorHandler = $prev ?: true;
}
}
public function registerFatalHandler($level = null, $reservedMemorySize = 20)
{
register_shutdown_function(array($this, 'handleFatalError'));
$this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
$this->fatalLevel = $level;
}
// [...]
}
Run Code Online (Sandbox Code Playgroud)
我需要为生产处理致命错误,而是显示静态样式的503 Service Unavailable HTML输出.这肯定是"捕捉致命错误"的合理方法.这就是我所做的:
我有一个自定义错误处理函数"error_handler",它将在任何E_ERROR,E_USER_ERROR等上显示我的"503 service unavailable"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');
Run Code Online (Sandbox Code Playgroud)
在我的自定义error_handler函数中,如果错误是E_ERROR或E_USER_ERROR等.我也调用@ob_end_clean(); 清空缓冲区,从而删除PHP的"致命错误"消息.
请注意严格的isset()检查和@沉默函数,因为我们不希望我们的error_handler脚本生成任何错误.
仍然同意keparo,捕捉致命错误确实打败了"致命错误"的目的,所以它并不是真正打算让你做进一步的处理.不要在此关闭过程中运行任何mail()函数,因为您肯定会备份邮件服务器或收件箱.而是将这些事件记录到文件并安排cron来查找这些error.log文件并将它们邮寄给管理员.
PHP有致命的致命错误.它们被定义为E_RECOVERABLE_ERROR.PHP手册将E_RECOVERABLE_ERROR描述为:
可捕获的致命错误.它表示发生了可能存在危险的错误,但未使引擎处于不稳定状态.如果错误未被用户定义的句柄捕获(另请参见set_error_handler()),则应用程序将中止,因为它是E_ERROR.
您可以使用set_error_handler()并检查E_RECOVERABLE_ERROR 来"捕获"这些"致命"错误.我发现捕获此错误时抛出异常很有用,然后您可以使用try/catch.
这个问题和答案提供了一个有用的例子:如何在PHP类型提示中捕获"可捕获的致命错误"?
但是,E_ERROR错误可以处理,但不会在引擎处于不稳定状态时恢复.
获取当前error_handler方法的一个很好的技巧=)
<?php
register_shutdown_function('__fatalHandler');
function __fatalHandler()
{
$error = error_get_last();
//check if it's a core/fatal error, otherwise it's a normal shutdown
if($error !== NULL && $error['type'] === E_ERROR) {
//Bit hackish, but the set_exception_handler will return the old handler
function fakeHandler() { }
$handler = set_exception_handler('fakeHandler');
restore_exception_handler();
if($handler !== null) {
call_user_func($handler, new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));
}
exit;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我也不想注意,如果你打电话
<?php
ini_set('display_errors', false);
?>
Run Code Online (Sandbox Code Playgroud)
Php停止显示错误,否则错误文本将在错误处理程序之前发送到客户端
由于这里的大多数答案都是不必要的冗长,这里是我最高投票答案的非丑陋版本:
function errorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) {
//Do stuff: mail, log, etc
}
function fatalHandler() {
$error = error_get_last();
if($error) errorHandler($error["type"], $error["message"], $error["file"], $error["line"]);
}
set_error_handler("errorHandler")
register_shutdown_function("fatalHandler");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
349102 次 |
| 最近记录: |