Error handler...

By Concordia on Jun 22, 2005

Hey, this is my error handler (well, the standard one). It's multi-purpose, easy to understand, and to change from occurance to occurance only requires changes in three or so variables.

<?php

$err_h = array ('status' => 'DEVELOPMENT', 'email' => 'CaptSushekECF@aol.com', 'log' => 'error_log.html');  // Set status to either "DEVELOPMENT" or "LIVE", any other variables will not work properly; set email to the email address errors will be logged to during LIVE mode; set log to the location errors will be logged.

if (isset($err_h['status']) && isset($err_h['email']) && isset($err_h['log'])) {
    if ($err_h['status'] == 'LIVE') {
        $reporting = 0;
        $err = array (1 => $err_h['email'], 3 => $err_h['log']);
    } elseif ($err_h['status'] == 'DEVELOPMENT') {
        $reporting = 2047;
        $err = array (3 => $err_h['log']);
    } else {
        if (!headers_sent()) {
            echo '<html><head><title>Error in configuration file.</title></head><body><p>There is an error within the configuaration file, this script has been halted.</p></body></html>';
            exit();
       } else {
            '<p>There is an error within the configuration file, this script has been halted.</p></div></body></html>';
            exit();
       }
    }
} else {
    if (!headers_sent()) {
        echo '<html><head><title>Error in configuration file.</title></head><body><p>There is an error within the configuaration file, this script has been halted.</p></body></html>';
        exit();
    } else {
        '<p>There is an error within the configuration file, this script has been halted.</p></div></body></html>';
        exit();
    }
}

error_reporting ($reporting);

function the_error_handler ($e_number, $e_message, $e_file, $e_line) {
    global $err_h;
    global $err;
    global $reporting;
    $message = '<p style="background-color: red;"><b>' . date('l, F d, Y (g:i A)') . "</b>: An error has occured in the script <b>$e_file</b> on line <b>$e_line</b>.  The server states the problem is <b>$e_message</b>, <b>$e_number</b>.  {$GLOBALS['err_h']['status']}";
    $message2 = '<p style="background-color: red;">There has been an error, rest assured it has been logged and will be taken care of shortly.</p>';
    if ($GLOBALS['reporting'] >= 1) {
        echo $message;
    }
    echo $message2;
    foreach ($GLOBALS['err'] as $key => $value) {
        error_log ($key, $value, $message);
    }
}

set_error_handler('the_error_handler');

?>

Comments

Sign in to comment.
Concordia   -  Jun 24, 2005

Most certainly. To use it how it is, just take the third line:

$err_h = array (\'status\' => \'DEVELOPMENT\', \'email\' => \'CaptSushekECF@aol.com\', \'log\' => \'error_log.html\'); // Set status to either \"DEVELOPMENT\" or \"LIVE\", any other variables will not work properly; set email to the email address errors will be logged to during LIVE mode; set log to the location errors will be logged.

$err_h is an array that sets up how it works.

 Respond  
Hawkee   -  Jun 23, 2005

Can we get some usage instructions?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.