PHP Custom error handling

Source: Internet
Author: User
Tags define function php error

Custom error reporting can be handled completely bypassing the standard PHP error handler, so you can print error reports in your own format, or change the location where the error report is printed (The standard PHP error report is where an error occurs and appears at the location where it occurred). There are several scenarios where you can consider custom error handling.

★ Can write down the wrong information, in time to find some production environment problems.
★ can be used to mask errors. An error can leak some information to the user and is likely to be a tool for hackers to attack your site.
★ You can do the corresponding processing, put all error reports to the final script output, or error can show jump to the pre-defined error page, provide a better user experience, if necessary, you can also in the custom error handler, according to the situation to terminate the script run.
★ As a debugging tool, sometimes you have to debug something while running the environment, but you don't want to affect the users you're using.

You typically use the Set_error_handler () function to set a user-defined error-handling function that creates a user's own error-handling method during runtime, returns an old error handler, and returns null if it fails. The function has two parameters, where the first parameter is required and a callback function is required to specify the function to run when an error occurs. This callback function must declare 4 parameters, otherwise invalid, in the order of "There is Error", "error message", "Error File" and "Error line number". The second parameter of the Set_error_handler () function is optional, which specifies which error reporting level now displays user-defined errors. The default is "E_all". Examples of custom error handling are as follows:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 <?php error_reporting(0); //屏蔽程序中的错误//定义Error_Handler函数,作为set_error_handler()函数的第一个参数“回调”function error_handler($error_level,$error_message,$file,$line){$EXIT =FALSE;switch($error_level){//提醒级别case E_NOTICE:case E_USER_NOTICE:$error_type = ‘Notice‘;break;//警告级别case E_WARNING:case E_USER_WARNING:$error_type=‘warning‘;break;//错误级别case E_ERROR:case E_USER_ERROR:$error_type=‘Fatal Error‘;$EXIT = TRUE;break;//其他未知错误default:$error_type=‘Unknown‘;$EXIT = TRUE;break; }//直接打印错误信息,也可以写文件,写数据库,反正错误信息都在这,任你发落printf("<font color=‘#FF0000‘><b>%s</b></font>:%s in<b>%s</b> on line <b>%d</b><br>\n",$error_type, $error_message, $file, $line);//如果错误影响到程序的正常执行,跳转到友好的错误提示页面if (TURE==$EXIT){echo ‘<script>location = "err.html";</scrpit>‘;}}//这个才是关键点,把错误的处理交给error_handle()set_error_handler(‘error_handler‘);//使用未定义的变量要报notice的echo $novar;//除以0要报警告的echo 3/0;//自定义一个错误trigger_error(‘Trigger a fatal error‘,E_USER_ERROR);?>

In this case, all printed error reports are output in their own format, but there is a point where the system directly reports fatal error is not captured, because the system cannot teach you such a big mistake. It is necessary to resolve this error, so the system will terminate the program directly. Using the Set_error_handler () function is a good way to solve the conflict between security and debugging convenience, and you can also take a little thought to make the error prompt more beautiful to match the style of the website. But pay attention to two points.
①e_error, E_parse, E_core_error, e_core_warning, E_compile_error, and e_compile_warning are not handled by this handle, which means they are displayed in the most primitive way. However, these errors are either compiled or the PHP kernel is faulty and will not normally occur.
After ② uses Set_error_handler (), the error_reporting () will fail. That is, all errors (except for the above errors) will be taught to define function handling.

>> This article fixed link: http://php.ncong.com/php_course/wrong/zidingyiwrong.html

>> reprint Please specify: Ntshongwana PHP August 05, 2014 Yuncon PHP Learning tutorial Published

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.