English 中文(简体)
How can I send all php errors run on one page to an email?
原标题:
  • 时间:2009-11-09 18:03:28
  •  标签:
  • 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

最佳回答

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 . "
";
        }
        mail(  to@example.com ,  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.

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()





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签