Use PHP to instantly capture errors in PHP and send email notifications. code _ PHP Tutorial

Source: Internet
Author: User
Use PHP to instantly capture PHP errors and send email notifications. PHP developers all know that the most worrying thing is that there are some exceptions or errors in the program. if these conditions are output to the user's screen, the user will be frightened and even lost their work, if anyone who develops PHP knows, the most worrying thing is that there are some exceptions or errors in the program. if these conditions are output to the user's screen, the user will be frightened or even lost in the work, if it is not output to the screen, you have to find a way to record the log, but it seems that not everyone has the habit of viewing the error log, in order to solve this embarrassing problem, so I wrote this code, the intention is to capture the error content when the php program we wrote fails and send it to our email.

First look at the effect:


The code is as follows:


Define ('sys _ debug', false );
IF (SYS_DEBUG ){
Ini_set ('display _ errors ', 'on ');
Error_reporting (E_ALL); // use this setting after going online (E_ERROR | E_WARNING | E_PARSE );
} Else {
Ini_set ('display _ errors ', 'off ');
Error_reporting (0 );
}

// Error capture
Register_shutdown_function ('fun: error ');

Class Fun {

/**
Common Error Handling
Parameters:
Content to be output, whether to terminate the execution program
Note:
This function can be used to output custom error content when values are passed.
In addition, you can use Register_shutdown_function to automatically capture error content and send the captured error content to the Email.
Register_shutdown_function is used to call a function when the program is executed or an error occurs midway through.
If it is called when an error is automatically captured, the last error content will be obtained. If no error content is found, it will jump out.
Return value:
The content is directly output 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! = '') {// Manually called
$ M =' Note:'. $ M;
Echo Strtr ($ ErrTpl, Array ('{$ M}' => $ M); unSet ($ ErrTpl );
IF ($ E === True) {Die ();}
Return;
} Else {// called when an error occurs when the program is automatically crawled after execution
$ M = error_get_last (); // get the final error
IF (! Is_array ($ M) Or Count ($ M) <4) {Unset ($ M); Return ;}
IF (! File_Exists ($ M ['file']) {Unset ($ M); Return ;}

// Obtain five lines of key error code. If no content is obtained, 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 =' An error is automatically caught!

Error description:
'. $ M ['file'].'The '. $ M ['line'].'The row type is '. $ M ['type'].'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 ['SP'], 'warning:'. $ G ['tit']. 'PHP program error! ', $ M) And SYS_DEBUG === True ){
Throw new Exception ('Warning: '. $ G ['tit'].' PHP program error!

'. $ M );
}
IF (SYS_DEBUG) {Echo $ M ;}
UnSet ($ E, $ M, $ G );
Die ();
}
}
/**
Send email
Parameters:
Recipient, Mail title (line break is not allowed), Mail content (lines and lines must be separated by \ n, each line cannot exceed 70 characters)
Note:
Call PHP built-in function Mail to send emails
Return value:
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); // if a full stop is found at the beginning of a line in Windows, it will be deleted. to avoid this problem, replace a single full stop with two full 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.