tags:

views:

92

answers:

4

Basically I have a single page on my site that I want any php erorrs, warnings, etc to be sent to me in an email every time the script is run.

Edit: this must be code that is placed on the page, not an edit to php_ini or anything like that.

Edit 2: this needs to catch ALL errors and send ALL errors in one email at the end of the script

+1  A: 

You can create a custom function for set_error_handler()

Kendall Hopkins
Example please...
Citizen
Plenty of examples in the docs: http://php.net/set_error_handler
ceejayoz
Read the manual, man! http://php.net/set_error_handler A simplistic example is pretty useless because of corner cases, and the full code is a lot of work of finding and debugging corner cases. Corner cases include startup errors, file upload errors, the script getting killed in an unexpected way etc
gnud
I dont see an example in the manual that emails alls errors.
Citizen
+1  A: 

The best you can do is probably to write an error handler, and setting that with set_error_handler. However, this will not handle all possible errors.

Magnus Nordlander
+5  A: 

you'll need to setup an error handler and register a shutdown function to do the mailing. in a very oversimplified example that could look something like this:

<?php

$__errors = array();
function my_error_handler($code, $message, $file, $line) {
    global $__errors;
    $__errors[] = sprintf('"%s" (%s line %s)', $message, $file, $line);
}
set_error_handler( 'my_error_handler', E_ALL );

function send_rror_log() {
    global $__errors;

    if ( count( $__errors ) > 0 ) {
     foreach ( $__errors as $error ) {
      $body . $error . "\n";
     }
     mail( '[email protected]', 'error log', $body );
    }
}
register_shutdown_function( 'send_error_log' );

?>
Kris
+2  A: 

If you are trying to catch problems with the code, it may be more efficient to just look at your webserver error logs (given that you have access). If you want these in digest form, you can write a cron job to mail you each day (or whatever).

If you don't have access to the error logs, then writing an error handler and using set_error_hander() is your best bet. I'd still suggest having the error handler write to a log file rather than emailing you. If your site gets any traffic at all your email box will be over-full in no time.

dnagirl