Use PHP to instantly capture errors in PHP and send an email notification implementation Code _php tutorial

Source: Internet
Author: User
PHP Development of friends know, in fact, the most worrying is that there are some exceptions or errors in the program, these conditions if the output to the user's screen will be frightened users, and even lost their work, if not output to the screen must find a way to log, but it seems not everyone has the habit of viewing the error log, To solve this embarrassing problem, so I wrote this code, the intention is that when we write a PHP program error when the wrong content is captured and sent to our email.

First look at the effect:


Copy CodeThe code is as follows:
Define (' Sys_debug ', false);
IF (Sys_debug) {
Ini_set (' display_errors ', ' on ');
Error_reporting (E_all);//Use this setting after online error_reporting (E_error | e_warning | E_parse);
}else{
Ini_set (' display_errors ', ' off ');
error_reporting (0);
}

Error trapping
Register_shutdown_function (' Fun::error ');

Class fun{

/**
General error Handling
Parameters:
What to output, whether to terminate the execution of the program
Description
This function can be used to output custom error content when there is a pass value
In addition, the register_shutdown_function can be used to automatically crawl the error content and send the wrong content to the email.
The mechanism for register_shutdown_function is to invoke a function when the program finishes or midway through an error
If it is called when an auto-fetch error occurs, the last error is made, and if no error is found, the message jumps out
Return:
Content will be output directly to the screen or email
Usage:
Fun::error (' error content ');
Fun::error (' Error content ', False);
/**/
Public Static Function Error ($M = ', $E =true) {
$ERRTPL = '
{$M}
';

$M =trim ($M);
IF ($M! = ") {//Manual call
$M = ' Note:'. $M;
Echo strtr ($ERRTPL, Array (' {$M} ' = = $M)); UnSet ($ERRTPL);
IF ($E ===true) {die ();}
Return;
Called when the}else{//program executes an automatic fetch error
$M =error_get_last ();//Obtain the last error generated
IF (! Is_array ($M) Or Count ($M) <4) {Unset ($M); Return;}
IF (! File_exists ($M [' file ']) {Unset ($M); Return;}

Get 5 lines of error critical code, if the content is not available, indicating that the error file does not exist
$E =array_slice (File ($M [' file ']), ($M [' line ']-4), 5);
IF (! Is_array ($E)) {Unset ($M, $E); Return;}

$E [' M ']= ';
for ($i =0; $i <5; $i + +) {
$E [$i]=isset ($E [$i])? $E [$i]: ';
$E [' M '].= ';
$E [' M '].= ($i ==3)? ' '. ($M [' line ']-3) + ($i + 1)). '': (($M [' line ']-3) + ($i + 1)];
$E [' M '].= ': '. Htmlspecialchars ($E [$i],ent_quotes, ' UTF-8 '). '
';
}
$E =& $E [' M '];

$M = ' Auto Catch error generated!

Error Description:
'. $M [' file ']. 'The first '. $M [' line ']. 'The row appears with the type '. $M [' type ']. 'The error:
'. $M [' message ']. '

Key code:
'. $E. '
'. Self::now (' y-m-d h:i:s ', Time ()). '
';

$M =strtr ($ERRTPL, Array (' {$M} ' = $M)); UnSet ($ERRTPL);

$G =SEFT::GETG (' SYS ', ' config ');
IF (!self::mail2 ($G [' Spe '], ' Warning: '. $G [' tit ']. ' PHP program Error! ', $M) and Sys_debug===true) {
throw new Exception (' Warning: '. $G [' tit ']. ' PHP program error occurred!

'. $M);
}
IF (sys_debug) {Echo $M;}
UnSet ($E, $M, $G);
Die ();
}
}
/**
Send e-mail
Parameters:
Recipient, message header (no newline characters), message contents (lines must be separated by \ n, each line cannot exceed 70 characters)
Description
Call PHP built-in function mail to send e-mail
Return:
Returns a Boolean value
Usage:
$IsSend =fun::mail2 ($email, $tit, $msg);
/**/
Public Static Function Mail2 ($to, $tit, $msg) {
IF (Filter_var ($to, filter_validate_email) = = ") {
throw new Exception (' E-mail address error! ');
}

$tit = ' =? UTF-8? B? '. Base64_encode ($tit). '? =';
$msg = Str_replace ("\ n.", "\ n..", $msg); If Windows finds a period at the beginning of a line, it will be deleted, to avoid the issue of replacing a single period with two periods

Return Mail ($to, $tit, $msg, ' from: '. SEFT::GETG (' Config/sys/mal '). " \ n "." Content-type:text/html;charset=utf-8 ');
}
}

http://www.bkjia.com/PHPjc/326333.html www.bkjia.com true http://www.bkjia.com/PHPjc/326333.html techarticle PHP Development of friends know, in fact, the most worrying is that there are some exceptions or errors in the program, these conditions if the output to the user's screen will be frightened users, and even lost their work, if ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.