1

我将如何编写一个 PHP 代码,每次我收到一个电子邮件时都会向我发送一封电子邮件

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14373306 bytes) in on line <b>443</b><br />.

我正在运行一个脚本,其中用户具有此 URL,它将由我的代码处理,但这里的问题是有时它会导致Fatal error Allowed memory size exhausted. 我想向我发送一封电子邮件,告诉我同时存在这个错误是哪个 URL 导致了该错误。

所以逻辑是这样的。

if( error == "Fatal Error Allowed memory Size" ) {
 mail("myemail@email.com", "Fatal Error - URL: http://google.com");
}

我希望指示很清楚。您的帮助将不胜感激和奖励!

谢谢!:-)

4

2 回答 2

4

你可以看看使用register_shutdown_function(). 它可以用来执行E_ERROR(致命错误)

register_shutdown_function('shutdown');

function shutdown()
{
  if(!is_null($e = error_get_last()))
  {
    mail("myemail@email.com", "Fatal Error - ". var_export($e, true));
  }
}

但是,我会在上面的评论中附和一些想法,即最好使用日志监控来处理。

于 2012-11-16T16:40:22.723 回答
0
Init following function inside your php file.

register_shutdown_function('mail_on_error'); //inside your php script

/** If  any file having any kind of fatal error, sln team will be notified when cron will
become fail : following is the name of handler and its type
E_ERROR: 1 | E_WARNING: 2 | E_PARSE: 4 | E_NOTICE: 8
*/

function mail_on_error() {
    global $objSettings;

    $error = error_get_last();
    //print_r($error);    

  if ($error['type'] == 1) {

        // update file path into db
        $objSettings->update_records($id=1,array('fatal_error' => json_encode($error)));

        $exception_in_file_path = __FILE__;
        fatal_error_structure($exception_in_file_path);

  }// end if

}// end mail_on_error

fatal_error_structure should be defined on some global location. like function.inc.php. This will send an email to registered user.


function fatal_error_structure($exception_in_file_path){

        $subject = "FATAL: Cron Daemon has failed";

    sln_send_mail(nl2br("Please check $exception_in_file_path, This cron having FATAL error."), 
        $subject, 'email@address.com',$name_reciever='', 'text/plain');

}// end fatal_error_structure

vKj
于 2014-04-08T11:24:17.890 回答