Forum Moderators: coopster

Message Too Old, No Replies

PHP error handling

way to email myself all errors in my PHP script?

         

meingalls

12:44 am on Mar 2, 2007 (gmt 0)

10+ Year Member



I am about to turn my PHP code over to production. I don't want errors displaying on the screen for all site visitors to see (and be confused by!), but at the same time, I still need to know about them and fix them.

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!

RonPK

1:14 pm on Mar 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a bit of script I'm using:


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.

RonPK

1:16 pm on Mar 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for the long lines. I used a combination of
pre
and
small
, which worked fine in preview mode...

meingalls

5:12 pm on Mar 2, 2007 (gmt 0)

10+ Year Member



Thank you, RonPK. This is great!

meingalls

6:16 pm on Mar 2, 2007 (gmt 0)

10+ Year Member



RonPK,

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!

jatar_k

6:21 pm on Mar 2, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at the link RonPK poster for set_error_handler, most of the information you need should be there

meingalls

6:26 pm on Mar 2, 2007 (gmt 0)

10+ Year Member



Got it! Thank you!