我可以set_error_handler()用来捕获大多数PHP错误,但不适用于致命(E_ERROR)错误,例如调用不存在的函数。还有另一种捕获这些错误的方法吗?
我正在尝试要求mail()所有错误,并且正在运行PHP 5.2.3。
我可以set_error_handler()用来捕获大多数PHP错误,但不适用于致命(E_ERROR)错误,例如调用不存在的函数。还有另一种捕获这些错误的方法吗?
我正在尝试要求mail()所有错误,并且正在运行PHP 5.2.3。
Answers:
使用记录致命错误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);
                    我只是想出了这个解决方案(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');
在预定义常量中定义了不同的错误类型。
If an error handler (see set_error_handler ) successfully handles an error then that error will not be reported by this function.”
                    register_shutdown_function()必须早于任何致命错误。use_1T_memory(); /* memory exhausted error here! */ register_shutdown_function('shutDownFunction');将无法按预期工作。
                    PHP不提供捕获致命错误并从中恢复的常规方法。这是因为在致命错误之后通常不应恢复处理。匹配输出缓冲区的字符串(如PHP.net上原始文章所建议的那样)绝对是不明智的。简直就是不可靠。
从错误处理程序方法中调用mail()函数也被证明是有问题的。如果您遇到很多错误,则邮件服务器将充满工作,并且收件箱会很粗糙。为避免这种情况,您可以考虑运行cron以定期扫描错误日志并相应地发送通知。您可能还想研究系统监视软件,例如Nagios。
说说有关注册关闭功能的一点:
您可以注册一个关闭功能,这确实是一个很好的答案。
这里的要点是,我们通常不应该尝试从致命错误中恢复,尤其是不要对输出缓冲区使用正则表达式。我正在回答已接受的答案,该答案链接到php.net上的一个建议,此建议已更改或删除。
该建议是在异常处理期间对输出缓冲区使用正则表达式,并且在发生致命错误(通过与您可能期望的任何已配置错误文本的匹配检测到)的情况下,请尝试进行某种恢复或继续处理。这不是推荐的做法(我相信这也是为什么我也找不到原始建议的原因。我要么忽略了它,要么php社区拒绝了它)。
可能值得注意的是,在调用输出缓冲回调之前,较新版本的PHP(约5.1)似乎更早调用了shutdown函数。在版本5和更早的版本中,顺序相反(输出缓冲回调后跟shutdown函数)。另外,由于大约是5.0.5(比发问者的版本5.2.3早得多),因此在调用已注册的关闭功能之前,对象已被卸载,因此您将无法依靠内存中的对象来执行操作很多东西。
因此,注册一个关闭功能很好,但是应该由关闭功能执行的任务种类可能仅限于少量的关闭程序。
对于那些偶然发现此问题并在最初接受的答案中看到建议的人,这里的重点只是一些智慧之词。不要正则表达式输出缓冲区。
好吧,似乎有可能以其他方式捕获致命错误:)
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;
}
              致命错误或者可恢复的致命错误,现在抛出的情况下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
}
或者,您可以使用Throwable接口来捕获所有异常。
例:
<?php
    try {
        undefinedFunctionCall();
    } catch (Throwable $e) {
        // Handle error
        echo $e->getMessage(); // Call to undefined function undefinedFunctionCall()
    }
              Fatal error: Trait 'FailedTrait' not found in使用时的错误的任何想法ReflectionClass?
                    include "filename.php"而不是包装在try块中,然后Throwablecatch块至少适用于ParseError。
                    我开发了一种方法来捕获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();
?>
              您无法捕获/处理致命错误,但可以记录/报告这些错误。为了快速调试,我修改了此简单代码的一个答案
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
    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
    }
?>
              /**
 * 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);
    }
}
此类允许您在需要时启动特定的ErrorHandler程序。然后,您也可以停止Handler。
使用此类,例如:
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;
链接到完整的类代码: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;
    }
    // [...]
}
              我需要处理生产中的致命错误,以显示静态样式的503 Service Unavailable 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的“致命错误”消息。
请特别注意严格的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) {
            // It is a 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;
        }
    }
?>
我还要指出的是,如果您致电
<?php
    ini_set('display_errors', false);
?>
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");
              我开发了此功能,以使“沙盒”代码可能会导致致命错误。由于从闭包中抛出的异常register_shutdown_function不会从致命错误之前的调用堆栈中发出,因此我被迫在此函数之后退出以提供统一的使用方法。
function superTryCatchFinallyAndExit( Closure $try, Closure $catch = NULL, Closure $finally )
{
    $finished = FALSE;
    register_shutdown_function( function() use ( &$finished, $catch, $finally ) {
        if( ! $finished ) {
            $finished = TRUE;
            print "EXPLODE!".PHP_EOL;
            if( $catch ) {
                superTryCatchFinallyAndExit( function() use ( $catch ) {
                    $catch( new Exception( "Fatal Error!!!" ) );
                }, NULL, $finally );                
            } else {
                $finally();                
            }
        }
    } );
    try {
        $try();
    } catch( Exception $e ) {
        if( $catch ) {
            try {
                $catch( $e );
            } catch( Exception $e ) {}
        }
    }
    $finished = TRUE;
    $finally();
    exit();
}
              在某些情况下,甚至致命错误也应被捕获(您可能需要进行一些清理才能优雅地退出,而不仅仅是死掉..)。
我在CodeIgniter中实现了pre_system挂钩应用程序中以便可以通过电子邮件获取致命错误,这有助于我查找未报告的错误(或在修复后报告的错误,因为我已经知道这些错误:)。
Sendemail检查是否已报告错误,以便它不会多次向您发送已知错误的垃圾邮件。
class PHPFatalError {
    public function setHandler() {
        register_shutdown_function('handleShutdown');
    }
}
function handleShutdown() {
    if (($error = error_get_last())) {
        ob_start();
        echo "<pre>";
        var_dump($error);
        echo "</pre>";
        $message = ob_get_clean();
        sendEmail($message);
        ob_start();
        echo '{"status":"error","message":"Internal application error!"}';
        ob_flush();
        exit();
    }
}