如何在php中使用register_shutdown_function()处理解析错误?

时间:2015-04-19 19:49:55

标签: php

我想使用register_shutdown_function()显示php错误,但是使用下面的脚本我无法处理解析错误:

<?php
register_shutdown_function('ShutDown');

echo "Hi" // generate  error ==  Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in file_naem on line 5;
echo "Hello";

?>


<?php 

function catchError($errno, $errstr, $errfile = '', $errline = ''){  

   echo "Eroor Type : " .$errno. "<br>";
   echo "Eroor Message : " . $errstr . "<br>";
   echo "Line Number : " . $errline;
   exit();
} 
function ShutDown(){
    $lasterror = error_get_last();
    if(in_array($lasterror['type'],Array( E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR, E_CORE_WARNING, E_COMPILE_WARNING, E_PARSE))){
        catchError($lasterror['type'],$lasterror['message'],$lasterror['file'],$lasterror['line']); 
    }
}   
?>

如何处理Parse error

3 个答案:

答案 0 :(得分:1)

只有在包含或需要的脚本中出现解析错误时才能捕获它们。 (另见https://stackoverflow.com/a/1900272/2123530

所以,对不起,这不会像你那样工作,但可以这样工作:

<?php
register_shutdown_function('ShutDown');

include 'include.php'; 

function catchError($errno, $errstr, $errfile = '', $errline = ''){

    echo "Eroor Type : " .$errno. "<br>";
    echo "Eroor Message : " . $errstr . "<br>";
    echo "Line Number : " . $errline;
    exit();
} 
function ShutDown(){
    $lasterror = error_get_last();
    if(in_array($lasterror['type'],Array( E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR, E_CORE_WARNING, E_COMPILE_WARNING, E_PARSE))){
        catchError($lasterror['type'],$lasterror['message'],$lasterror['file'],$lasterror['line']);
    }
}   
?>

include.php

的内容
<?php 
echo "Hi" // generate  error ==  Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in file_naem on line 5;
echo "Hello";
?>

答案 1 :(得分:0)

htaccess的:

php_value auto_prepend_file /www/register_shutdown_function.php

register_shutdown_function.php:

<?php
// (c) 2015 http://Gutt.IT/
error_reporting(-1);
ini_set('display_errors', 1);
define('WEBMASTER_EMAIL', 'john.doe@example.org');
function error_type($id) {
    switch($id) {
        case E_ERROR:// 1
            return 'E_ERROR';
        case E_WARNING:// 2
            return 'E_WARNING';
        case E_PARSE:// 4
            return 'E_PARSE';
        case E_NOTICE:// 8
            return 'E_NOTICE';
        case E_CORE_ERROR:// 16
            return 'E_CORE_ERROR';
        case E_CORE_WARNING:// 32
            return 'E_CORE_WARNING';
        case E_COMPILE_ERROR:// 64
            return 'E_COMPILE_ERROR';
        case E_COMPILE_WARNING:// 128
            return 'E_COMPILE_WARNING';
        case E_USER_ERROR:// 256
            return 'E_USER_ERROR';
        case E_USER_WARNING:// 512
            return 'E_USER_WARNING';
        case E_USER_NOTICE:// 1024
            return 'E_USER_NOTICE';
        case E_STRICT:// 2048
            return 'E_STRICT';
        case E_RECOVERABLE_ERROR:// 4096
            return 'E_RECOVERABLE_ERROR';
        case E_DEPRECATED:// 8192
            return 'E_DEPRECATED';
        case E_USER_DEPRECATED:// 16384
            return 'E_USER_DEPRECATED';
    }
    return 'UNKNOWN';
}
function error_alert() {
    // send alert
    if(!is_null($e = error_get_last())) {
        $type = error_type($e["type"]);
        if (strpos($type, 'ERROR') !== false || strpos($type, 'PARSE') !== false) {
            mail(WEBMASTER_EMAIL, $type . ' in ' . $e['file'] . ' at line ' . $e['line'], $e['message']);
        }
    }
}
register_shutdown_function('error_alert');
?>

答案 2 :(得分:-1)

error_reporting(-1); //show all errors

ini_set('display_errors', 'Off'); //don't show message directly

//define your function to handle and how to display the errors && exceptions
register_shutdown_function('your_handle_func'); 

也许您会使用set_error_handler()set_exceptions_handler()