4

I want to display php error using register_shutdown_function(), but using bellow script i can not handle Parse error:

<?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']); 
    }
}   
?>

How to Handle Parse error?

0

3 Answers 3

1

Parse errors can only be caught if they occur in scripts included or required. (also see https://stackoverflow.com/a/1900272/2123530)

So, sorry, this won't work the way you did it but can work this way :

<?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']);
    }
}   
?>

Content of 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";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

ParseErrors in PHP 7 are exceptions and should be handled with an exception handler. Register an exception handler with set_exception_handler() and check the incoming exception when your included script contains a parse error. In PHP 7 the ParseError only makes it to the shutdown function if no exception handler is defined. See also stackoverflow.com/questions/47728546/…
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', '[email protected]');
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');
?>

Comments

-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'); 

maybe you will use set_error_handler() and set_exceptions_handler()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.