PHP Custom Error Page

Everyone says that “Enabling errors to be displayed” on an active site is bad (due to some security issues).

Now we must consider two cases:

  • Site is in debug mode
  • Site is not in debug mode

Now, for case No. 1:

We want to see errors. How?

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

There is nothing easier. We can also set up an error handler for all errors except Parse and Fatal.

Instead, if case # 2:

We would like to be able to deactivate messages:

ini_set('error_reporting', 0);
ini_set('display_errors', 0);

. , , "Hei man, - f ** ked up. , , ". set_error_handler() , . :

1. , - ? , ini_set('error_reporting', 0); ini_set('display_errors', 0); PHP, ?

:

2. , set_error_handler() . , . ( , , , , ). - ?

+5
2

, / uncaught. , , , , , , . - . , , , , .

:

<?php
function redirect_on_error(){
    if(!defined('EVERYTHING_WENT_OK')){
        ob_end_clean();
        header('Location: error.html');
    }
}

register_shutdown_function('redirect_on_error');

ob_start();

include 'some/working/code.php';

echo "Now I'm going to call undefined function or throw something bad";

undefined_function();
throw new Exception('In case undefined function is defined.');    

define('EVERYTHING_WENT_OK', TRUE);
exit;
+5

1: , - ?

, , , . , PHP 500, , , -, , . , PHP, , Apache.

2: , set_error_handler() .

, . , , .

0

All Articles