Use PHP to instantly capture errors in PHP and send email notifications to implement code _php tips

Source: Internet
Author: User
Tags php error php programming
All the friends who develop PHP know that in fact, the most worry is that there are some exceptions or errors in the program, if the output to the user's screen will frighten the user, and even lost the job, if not output to the screen to find ways to record the log, but it seems not everyone has the habit of viewing the error log, To solve this embarrassing problem, I wrote this code, which is meant to capture the wrong content when we write a PHP program and send it to our email.

First look at the effect:


Copy Code code as follows:

Define (' Sys_debug ', false);
IF (Sys_debug) {
Ini_set (' display_errors ', ' on ');
Error_reporting (E_all)//On line use this setting error_reporting (E_error | e_warning | E_parse);
}else{
Ini_set (' display_errors ', ' off ');
error_reporting (0);
}

Error capture
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, it can also cooperate with register_shutdown_function to realize automatic crawl error content, and will crawl the wrong content to send to the email
The register_shutdown_function mechanism is to call the function when the program is finished or midway through the error
If it is invoked when an automatic crawl error occurs, the content of the last error is obtained, and if there is no error, the content jumps out
Return:
Content will be exported directly to the screen or email
Usage:
Fun::error (' wrong content ');
Fun::error (' Error content ', false);
/**/
Public Static Function Error ($M = ', $E =true) {
$ERRTPL = '
$M =trim ($M);
IF ($M!= ') {//Manual call
$M = ' <b> note:</b> '. $M;
Echo strtr ($ERRTPL, Array (' {$M} ' => $M)); UnSet ($ERRTPL);
IF ($E ===true) {Die ();}
return;
Call when the}else{//program completes an automatic crawl error
$M =error_get_last ()//To make the final error
IF (! Is_array ($M) Or Count ($M) <4) {Unset ($M); return;}
IF (! File_exists ($M [' file '])) {Unset ($M); return;}

Get 5 lines of error key code, if not the content, 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)? ' <b> '. ($M [' line ']-3] + ($i + 1)). ' </b> ': (($M [' line ']-3] + ($i + 1));
$E [' M '].= ': '. Htmlspecialchars ($E [$i],ent_quotes, ' UTF-8 '). ' <br> ';
}
$E =& $E [' M '];

$M = ' <b> automatically captures error!</b><br><br><b> error:</b><br> <b> '. $M [' file ']. ' </b> <b> '. $M [' line ']. ' </b> line appears with Type <b> '. $M [' type ']. ' </b> error:<br> '. $M [' message ']. ' <br><br><b> key code:</b><br> '. $E. ' <br> ' Self::now (' y-m-d h:i:s ', Time ()). ' <br> ';

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

$G =SEFT::GETG (' SYS ', ' config ');
IF (!self::mail2 ($G [' Spe '], ' Warning: '. $G [' tit ']. ' PHP programming Error! ', $M) and Sys_debug===true) {
throw new Exception (' Warning: '. $G [' tit ']. ' PHP error!<br><br> '. $M);
}
IF (sys_debug) {Echo $M;}
UnSet ($E, $M, $G);
Die ();
}
}
/**
Send e-mail
Parameters:
Recipients, message headers (no line breaks), message content (must be delimited between rows and rows, no more than 70 characters per line)
Description
Invoke PHP built-in function mail to send an email
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 (' Email address error! ');
}

$tit = ' =? UTF-8? B? '. Base64_encode ($tit). =';
$msg = Str_replace ("\ n.", "\ n..", $msg); Windows will be deleted if a period is found at the beginning of a line to avoid this problem by 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 ');
}
}

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.