Question

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

Was it helpful?

Solution

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_error_log() {
    global $__errors;

    if ( count( $__errors ) > 0 ) {
        foreach ( $__errors as $error ) {
            $body . $error . "\n";
        }
        mail( 'to@example.com', 'error log', $body );
    }
}
register_shutdown_function( 'send_error_log' );

?>

OTHER TIPS

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.

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.

You can create a custom function for set_error_handler()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top