I have a separate config.php file which defines some constants and functions which potentially all other scripts will use. Then at the start of each of my scripts I include the config.php file.
config.php
Code:
<?php
define('LIVE', TRUE); //is set to false when the site is being tested
define('EMAIL', 'email@example.com'); //put your email address here
//create function to deal with errors
function my_error_handler ($errNum, $errMsg, $errFile, $errLine, $errVars) {
$message = "<p>An error occurred in script '$errFile' on line $errLine: $errMsg\n<br />";
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
$message .= "<p>" . print_r ($errVars, 1) . "</p>\n</p>";
//handle the error depending on whether the site is live or not
if (!LIVE) { //if the site is not live, display the error message to the screen - useful for debugging
echo '<div>' . $message . '</div><br />';
} else { // else, send the error message to the email address defined above
mail(EMAIL, 'Site Error!', $message, 'From: email@site.com'); //include an email subject and from address - to identify which site is giving the error
if ($e_number != E_NOTICE) { //and display an error message to the user only if the error is more serious than a warning.
echo '<div class="error">A system error occurred. We apologise for the inconvenience.</div><br />';
}
}
}//end function declaration
set_error_handler ('my_error_handler'); //set up the error handler function
?>
Bookmarks