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
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);
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.
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.
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.
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' );
?>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With