Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send all php errors run on one page to an email?

Tags:

php

php-5.3

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

like image 679
Citizen Avatar asked Nov 09 '09 18:11

Citizen


People also ask

How do I display PHP errors on a page?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

How can I send error message from PHP to HTML?

By default, PHP sends an error log to the server's logging system or a file, depending on how the error_log configuration is set in the php. ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.

Which of the following PHP directive is used to display all the errors except notices?

error_reporting: It displays all level errors except E-NOTICE, E-STRICT, and E_DEPRECATED level errors. display_errors: By default, the value of display_errors is off.

How do I log errors in PHP?

The ini_set(“log_errors”, TRUE) command can be added to the php script to enable error logging in php. The ini_set('error_log', $log_file) command can be added to the php script to set the error logging file. Further error_log($error_message) function call can be used to log error message to the given file.


2 Answers

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( '[email protected]', 'error log', $body );
    }
}
register_shutdown_function( 'send_error_log' );

?>
like image 181
Kris Avatar answered Nov 14 '22 22:11

Kris


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.

like image 26
dnagirl Avatar answered Nov 14 '22 21:11

dnagirl