Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP to instantly catch errors in PHP and send email notifications

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use PHP to instantly catch errors in PHP and send email notifications. I hope you will get something after reading this article. Let's discuss it together.

Friends who develop PHP know that, in fact, what they are most worried about is some exceptions or errors in the program. If these conditions are output to the user's screen, they will frighten the user and even lose their job. If they are not output to the screen, they will have to find a way to record them in the log. But it seems that not everyone has the habit of viewing the error log. In order to solve this embarrassing problem, I wrote this code. The intention is to catch the error content when our php program goes wrong and send it to our email.

Let's first look at the effect:

The copy code is as follows:

Define ('SYS_DEBUG',false)

IF (SYS_DEBUG) {

Ini_set ('display_errors','on')

Error_reporting (E_ALL); / / use this setting Error_reporting (E_ERROR | E_WARNING | E_PARSE) after launch

} Else {

Ini_set ('display_errors','off')

Error_reporting (0)

}

/ / error capture

Register_shutdown_function ('Fun::Error')

Class Fun {

/ * *

General error handling

Parameters:

The content to be output, whether to terminate the execution of the program

Description:

This function can be used to output custom error content when a value is passed.

In addition, you can also cooperate with Register_shutdown_function to automatically grab the error content, and send the captured error content to the Email

The mechanism of Register_shutdown_function is to call the function when the program is finished or when something goes wrong.

If it is called when an automatic crawl error is made, the content of the last error will be obtained, and if no error content is found, it will jump out

Return:

The content will be output directly to the screen or Email

Usage:

Fun::Error ('error content')

Fun::Error ('error content', False)

/ * /

Public Static Function Error ($million dollars) {

$ErrTpl=' {$M}'

$M=Trim ($M)

IF ($Muggles calls') {/ / manual call

$$attention:. $M

Echo Strtr ($ErrTpl,Array ('{$M}'= > $M)); unSet ($ErrTpl)

IF ($E===True) {Die ();}

Return

} Else {/ / called when the program finishes crawling an error

$M=error_get_last (); / / get the final error

IF (! Is_array ($M) Or Count ($M) $M); unSet ($ErrTpl)

$G=seft::getG ('SYS','config')

IF (! self::Mail2 ($G ['Spe'],' warning:'. $G ['Tit']. PHP program error occurred!', $M) And SYS_DEBUG===True) {

Throw new Exception ('warning:'. $G ['Tit']. A PHP program error occurred!

'. $M)

}

IF (SYS_DEBUG) {Echo $M;}

UnSet ($E _ pen / M _ J / G)

Die ()

}

}

/ * *

Send an email

Parameters:

Recipient, message title (no newline characters), message content (lines must be\ nseparated, each line must not exceed 70 characters)

Description:

Call PHP built-in function Mail to send 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 ('wrong email address!')

}

$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, replace 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')

}

}

After reading this article, I believe you have a certain understanding of "how to use PHP to catch errors in PHP and send email notifications". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report