


Use PHP to instantly capture errors in PHP source code and send email notifications, javaemail sending_PHP tutorial
Jul 12, 2016 am 08:51 AMUse PHP to instantly capture errors in the PHP source code and send email notifications, javaemail sent
Friends who develop PHP know that in fact, the most worrying thing is the error in the program Some exceptions or errors. If these conditions are output to the user's screen, the user will be frightened and even lose their job. If they are not output to the screen, they have to find a way to record them in the log, but it seems that not everyone has the ability to check the errors. In order to solve this embarrassing problem, I wrote this code. Its purpose is to capture the error content when the php program we wrote makes an error and send it to our email.
Let’s take a look at the effect first:
<span>Define</span>('SYS_DEBUG',<span>false</span><span>); </span><span>IF</span><span>(SYS_DEBUG) { </span><span>ini_set</span>('display_errors','on'<span>); </span><span>Error_reporting</span>(<span>E_ALL</span>);<span>//</span><span>上線后使用該設(shè)定Error_reporting(E_ERROR | E_WARNING | E_PARSE);</span> }<span>Else</span><span>{ </span><span>ini_set</span>('display_errors','off'<span>); </span><span>Error_reporting</span>(0<span>); } </span><span>//</span><span>錯(cuò)誤捕捉</span> <span>Register_shutdown_function</span>('Fun::Error'<span>); </span><span>Class</span><span> Fun{ </span><span>/*</span><span>* 通用出錯(cuò)處理 參數(shù): 要輸出的內(nèi)容,是否終止執(zhí)行程序 說(shuō)明: 有傳值時(shí)該函式可以用來(lái)輸出自定義的錯(cuò)誤內(nèi)容 另外還可以配合Register_shutdown_function實(shí)現(xiàn)自動(dòng)抓取錯(cuò)誤內(nèi)容,並將抓取的錯(cuò)誤內(nèi)容發(fā)送到Email內(nèi) Register_shutdown_function的機(jī)制是程序執(zhí)行完畢或中途出錯(cuò)時(shí)調(diào)用函數(shù) 如果是自動(dòng)抓取錯(cuò)誤時(shí)被調(diào)用,則會(huì)取得最後一次出錯(cuò)的內(nèi)容,如果發(fā)現(xiàn)沒(méi)有錯(cuò)誤內(nèi)容則跳出 返回: 內(nèi)容會(huì)被直接輸出至螢?zāi)换駿mail內(nèi) 用法: Fun::Error('錯(cuò)誤內(nèi)容'); Fun::Error('錯(cuò)誤內(nèi)容',False); /*</span><span>*/</span> <span>Public</span> <span>Static</span> <span>Function</span> Error(<span>$M</span>='',<span>$E</span>=<span>True</span><span>){ </span><span>$ErrTpl</span>='<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><table cellspacing="0" cellpadding="0" border="0"><tr><td>; </span><span>$M</span>=<span>Trim</span>(<span>$M</span><span>); </span><span>IF</span>(<span>$M</span>!='') {<span>//</span><span>手工調(diào)用</span> <span>$M</span>=' <b>注意:</b> '.<span>$M</span><span>; </span><span>Echo</span> <span>Strtr</span>(<span>$ErrTpl</span>,<span>Array</span>('{$M}'=><span>$M</span>));<span>unSet</span>(<span>$ErrTpl</span><span>); </span><span>IF</span>(<span>$E</span>===<span>True</span>) {<span>Die</span><span>();} </span><span>Return</span><span> ; }</span><span>Else</span>{<span>//</span><span>程式執(zhí)行完畢自動(dòng)抓取錯(cuò)誤時(shí)調(diào)用</span> <span>$M</span>=error_get_last();<span>//</span><span>取得最後產(chǎn)生的錯(cuò)誤</span> <span>IF</span>(!<span>Is_array</span>(<span>$M</span>) Or <span>Count</span>(<span>$M</span>)<4) {<span>Unset</span>(<span>$M</span>);<span>Return</span><span> ;} </span><span>IF</span>(!<span>File_Exists</span>(<span>$M</span>['file'])) {<span>Unset</span>(<span>$M</span>);<span>Return</span><span> ;} </span><span>//</span><span>取得5行出錯(cuò)關(guān)鍵代碼,如果取不到內(nèi)容,說(shuō)明出錯(cuò)檔案不存在</span> <span>$E</span>=<span>Array_slice</span>(<span>File</span>(<span>$M</span>['file']),(<span>$M</span>['line']-4),5<span>); </span><span>IF</span>(!<span>Is_array</span>(<span>$E</span>)) {<span>Unset</span>(<span>$M</span>,<span>$E</span>);<span>Return</span><span> ;} </span><span>$E</span>['M']=''<span>; </span><span>For</span>(<span>$i</span>=0;<span>$i</span><5;<span>$i</span>++<span>) { </span><span>$E</span>[<span>$i</span>]=<span>isSet</span>(<span>$E</span>[<span>$i</span>]) ? <span>$E</span>[<span>$i</span>] : ''<span>; </span><span>$E</span>['M'].=' '<span>; </span><span>$E</span>['M'].=(<span>$i</span>==3) ? '<b>'.((<span>$M</span>['line']-3)+(<span>$i</span>+1)).'</b>' : ((<span>$M</span>['line']-3)+(<span>$i</span>+1<span>)); </span><span>$E</span>['M'].=': '.<span>Htmlspecialchars</span>(<span>$E</span>[<span>$i</span>],ENT_QUOTES,'UTF-8').'<br>'<span>; } </span><span>$E</span>=&<span>$E</span>['M'<span>]; </span><span>$M</span>='<b>自動(dòng)捕捉到有錯(cuò)誤產(chǎn)生!</b><br><br><b>錯(cuò)誤描述:</b><br> <b>'.<span>$M</span>['file'].'</b>的第<b>'.<span>$M</span>['line'].'</b>行出現(xiàn)了類(lèi)型為<b>'.<span>$M</span>['type'].'</b>的錯(cuò)誤:<br> '.<span>$M</span>['message'].'<br><br><b>關(guān)鍵代碼:</b><br>'.<span>$E</span>.'<br>'.self::now('Y-m-d H:i:s',<span>time</span>()).'<br>'<span>; </span><span>$M</span>=<span>Strtr</span>(<span>$ErrTpl</span>,<span>Array</span>('{$M}'=><span>$M</span>));<span>unSet</span>(<span>$ErrTpl</span><span>); </span><span>$G</span>=seft::getG('SYS','config'<span>); </span><span>IF</span>(!self::Mail2(<span>$G</span>['Spe'],'警告: '.<span>$G</span>['Tit'].' 出現(xiàn) PHP 程式錯(cuò)誤!',<span>$M</span>) And SYS_DEBUG===<span>True</span><span>){ </span><span>throw</span> <span>new</span> <span>Exception</span>('警告: '.<span>$G</span>['Tit'].' 出現(xiàn) PHP 程式錯(cuò)誤!<br><br>'.<span>$M</span><span>); } </span><span>IF</span>(SYS_DEBUG) {<span>Echo</span> <span>$M</span><span>;} </span><span>unSet</span>(<span>$E</span>,<span>$M</span>,<span>$G</span><span>); </span><span>Die</span><span>(); } } </span><span>/*</span><span>* 發(fā)送電郵 參數(shù): 收件人,郵件標(biāo)題(不可有換行符),郵件內(nèi)容(行與行之間必須用\n分隔,每行不可超過(guò)70個(gè)字符) 說(shuō)明: 調(diào)用PHP內(nèi)置函式Mail發(fā)送電郵 返回: 返回布爾值 用法: $IsSend=Fun::Mail2($email,$tit,$msg); /*</span><span>*/</span> <span>Public</span> <span>Static</span> <span>Function</span> Mail2(<span>$to</span>,<span>$tit</span>,<span>$msg</span><span>) { </span><span>IF</span>(Filter_var(<span>$to</span>,FILTER_VALIDATE_EMAIL)==''<span>){ </span><span>throw</span> <span>new</span> <span>Exception</span>('電郵地址錯(cuò)誤!'<span>); } </span><span>$tit</span>='=?UTF-8?B?'.<span>Base64_Encode</span>(<span>$tit</span>).'?='<span>; </span><span>$msg</span> = <span>str_replace</span>("\n.","\n..",<span>$msg</span>); <span>//</span><span>Windows如果在一行開(kāi)頭發(fā)現(xiàn)一個(gè)句號(hào)則會(huì)被刪掉,要避免此問(wèn)題將單個(gè)句號(hào)替換成兩個(gè)句號(hào)</span> <span>Return</span> <span>Mail</span>(<span>$to</span>,<span>$tit</span>,<span>$msg</span>,'From:'.seft::getG('config/SYS/Mal')."\n".'Content-Type:text/html;charset=utf-8'<span>); } }</span>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

When encountering the problem that header('Location:...') does not work, the common reasons and solutions are as follows: 1. There is output in advance, causing the header to fail. The solution is to ensure that there is no output before the jump, including spaces, HTML or echo; 2. There is excess output or UTF-8 BOM characters in the include or require file. The file encoding should be checked and saved as "UTF-8 BOM-free"; 3. It is recommended to use ob_start() to turn on the output buffer before the jump, and cooperate with ob_end_flush() to delay the output; 4. After the jump, be sure to add exit to prevent subsequent code execution; 5. Make sure that the header() function call is before all outputs.
