Developers of PHP know that the most worrying thing is that there are some abnormalities or mistakes in the program, these situations, if the user's screen is dropped to scare the user, or even lose the job, if you do not output to the screen will have to be recorded in the journal, but it does not seem that everyone has to look at the wrong Journal of the Habit, To understand the problem of this awkwardness embarrassed, so I wrote this code, which is meant to capture the bug content and send it to our email when the PHP program we wrote was wrong.
First look at the effect:
Define(' Sys_debug ',false);IF(sys_debug) {Ini_set(' Display_errors ', ' on ');error_reporting(E_all);//use the setting error_reporting after the line (E_error | e_warning | E_parse);}Else{Ini_set(' display_errors ', ' off ');error_reporting(0);}//Bug Captureregister_shutdown_function(' Fun::error ');Classfun{/** General Fault handling parameters: the content to be output, whether the end of the program says: There is a value when the function can be used to output the wrong content of the definition can also cooperate with register_shutdown_function to automatically crawl the wrong content, And will crawl the wrong content to send to the register_shutdown_function in the email system is the program after the completion or the middle of the wrong time to adjust the function if it is a self-fetching bug is adjusted, it will get the last mistake of the content, if there is no mistake inside to jump back: The content will be directly output to the screen or email usage: fun::error (' bug content '); Fun::error (' Wrong content ', False);/**/ Public Static FunctionError ($M= ",$E=True){$ERRTPL= ' ;$M=Trim($M);IF($M! = ") {//Manual Adjustments$M= ' <b> Note:</b> '.$M;Echo STRTR($ERRTPL,Array(' {$M} ' =$M));UnSet($ERRTPL);IF($E===True) { die();}Return ;}Else{//The program performs the auto crawl bug when$M=error_get_last ();//make the final mistake.IF(!Is_array($M) OrCount($M) {<4) {Unset($M);Return ;}IF(!file_exists($M[' File ']) {Unset($M);Return ;}//get 5 lines out of the wrong key code, if you do not get the content, stating that the wrong 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> auto Capture to bug!</b><br><br><b> description:</b><br> < B> '.$M[' file ']. ' </b> 's <b> '.$M[' line ']. ' </b> shows the type of <b> '.$M[' type ']. ' </b> Bug:<br> '.$M[' message ']. ' <br><br><b> keys:</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 program Bug! ',$M) and sys_debug===True){Throw New Exception(' Warning: '.$G[' Tit ']. ' PHP program Bug!<br><br> '.$M);}IF(Sys_debug) {Echo $M;}UnSet($E,$M,$G); die();}}/** Send e-mail parameters: recipient, email title (No line break), email content (line and line must be separated by \ n, each line cannot exceed 70 characters) stating: Adjusting with PHP internal function mail send back: Return to value usage: $IsSend =fun::mail2 ($ email, $tit, $msg);/**/ Public Static FunctionMAIL2 ($to,$tit,$msg) {IF(Filter_var ($to, filter_validate_email) = = "){Throw New Exception(' Email address is wrong! '));}$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 periodsReturn Mail($to,$tit,$msg, ' from: '. SEFT::GETG (' Config/sys/mal '). " \ n "." Content-type:text/html;charset=utf-8 ');}}
Use PHP to capture bugs in PHP's source code and send email notifications