Search code examples
phpapachefunctionfastcgifpm

register_shutdown_function() not working


I have 2 servers :

  • 1) PHP Version 7.0.28-1~dotdeb+8.1

System Linux online 3.16.0-4-amd64 #1 SMP Debian 3.16.51-2 (2017-12-03) x86_64 Server API FPM/FastCGI Virtual Directory Support disabled Configuration File (php.ini) Path /etc/php/7.0/fpm

  • 2) PHP Version 5.6.34-1~dotdeb+7.1

System Linux ovh 3.14.32-xxxx-std-ipv6-64 #6 SMP Wed Jan 20 18:22:32 CET 2016 x86_64 Build Date Mar 12 2018 09:28:40 Server API CGI/FastCGI Virtual Directory Support disabled Configuration File (php.ini) Path /etc/php5/cgi

On server 1 it works great when i have an error it call HTTP0.html,

<?php register_shutdown_function(function(){
$last_error = error_get_last();
if ( !empty($last_error) &&
    $last_error['type'] & (E_ERROR | E_COMPILE_ERROR | E_PARSE | E_CORE_ERROR | E_USER_ERROR)
)
{
    require_once(dirname(__FILE__).'/ErrorPages/HTTP0.html');
    exit(1);
}
});
echo phpinfo(); ?>

But on the server 2 : When i have an error it doesn't change anything :

Parse error: syntax error, unexpected ';' in /var/www....

I think the difference is FPM vs CGI

Thanks for your help


Solution

  • I find a solution to help everyone : I use PHP.ini to prepend a file (prepend.php) with this code

    <?php 
    error_reporting(-1);
    ini_set('display_errors', 0);
    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']);
                require_once(__DIR__.'/HTTP0.html');
                exit(1);
            }
        }
    }
    register_shutdown_function('error_alert');
    ?>