Forum Moderators: coopster
To turn off the errors on the screen, I have included at the beginning of each PHP page the following:
error_reporting(0);
As I read the documentation (http://us3.php.net/manual/en/ref.errorfunc.php), it appears that I can have errors emailed to me using the error_log function, but I am not sure how to detect that an error has occurred during the course of executing a script. Can someone please enlighten me? Perhaps even a sample script?
Thank you!
function myErrorHandler($errno, $errstr, $errfile, $errline) {
$out = '';
switch ($errno) {
case E_PARSE:
$out .= sprintf("Parse error. Error nr: %s\nError: %s\nFile: %s\nLine: %s\n\nStopping...", $errno, $errstr, $errfile, $errline);
exit(1);
break;
case E_ERROR:
$out .= sprintf("An error occured. Error nr: %s\nError: %s\nFile: %s\nLine: %s\n\nStopping...", $errno, $errstr, $errfile, $errline);
exit(1);
break;
case E_WARNING:
$out .= sprintf("Warning. Error nr: %s\nError: %s\nFile: %s\nLine: %s", $errno, $errstr, $errfile, $errline);
break;
case E_NOTICE:
$out .= sprintf("Notice. Error nr: %s\nError: %s\nFile: %s\nLine: %s", $errno, $errstr, $errfile, $errline);
break;
case E_STRICT:
$out .= sprintf("Strict mode problem. Error nr: %s\nError: %s\nFile: %s\nLine: %s", $errno, $errstr, $errfile, $errline);
break;
default:
$out .= sprintf("Some other error. Error nr: %s\nError: %s\nFile: %s\nLine: %s", $errno, $errstr, $errfile, $errline);
break;
}
if (!empty($out)) {
if (!doMail($out)) {
echo '<p>', nl2br($out), '</p>';
}
}
}
function doMail($text) {
$succes = false;
// restore to default error handling, in order to prevent recursive calling of myErrorHandler if mail() fails
restore_error_handler();
if (@mail ('webmaster@example.com', 'error on site', $text)) {
$succes = true;
}
set_error_handler('myErrorHandler');
return $succes;
}
set_error_handler('myErrorHandler');
IIRC I basically copied it from [php.net...] and added the mailing feature. The script will mail an error report. If mail() fails, the report will be output to stdout, i.e. to screen.
I find that I am getting emails for every variable reference that addresses an undefined variable. For example, if I have a statement that says:
if (@$foo) $bar = 1;
and $foo is not defined, I get an email error message. Given that I have prefaced the reference to $foo with the "@", so that it does not generate an error, I would not consider this an error.
Is there a way to modify the code you listed so that it will still generate an error if I didn't have the "@" but not if I do?
Thanks!