Detailed explanation of the use of php set_error_handler() function

怪我咯
Release: 2023-03-13 12:12:02
Original
1143 people have browsed it

set_error_handler() function sets the user-defined error handling function.

Note: If this function is used, the standard PHP error handler is bypassed and the user-defined error handler terminates the script via die() if necessary.

Note: If the error occurs before the script is executed (such as when a file is uploaded), the custom error handler will not be called because it has not been registered at that time.

Syntax

set_error_handler(errorhandler,E_ALL|E_STRICT);
Copy after login
Parameters Description
errorhandler Required. Specifies the name of the user error handling function.
E_ALL|E_STRICT Optional. Specifies what error reporting level is displayed for user-defined errors. The default is "E_ALL".

The method of using set_error_handler is as follows:

string set_error_handler ( callback error_handler [, int error_types])
Copy after login

Now we use custom error handling to filter out the actual path. Suppose there is a variable $admin, which we use to determine whether the visitor is an administrator (this determination can be made by IP or logged-in user ID)

//admin为管理员的身份判定,true为管理员。 //自定义的错误处理函数一定要有这4个输入变量$errno,$errstr,$errfile,$errline,否则无效。 function my_error_handler($errno,$errstr,$errfile,$errline) { //如果不是管理员就过滤实际路径 if(!admin) { $errfile=str_replace(getcwd(),"",$errfile); $errstr=str_replace(getcwd(),"",$errstr); } switch($errno) { case E_ERROR: echo "ERROR: [ID $errno] $errstr (Line: $errline of $errfile) \n"; echo "程序已经停止运行,请联系管理员。"; //遇到Error级错误时退出脚本 exit; break; case E_WARNING: echo "WARNING: [ID $errno] $errstr (Line: $errline of $errfile) \n"; break; default: //不显示Notice级的错误 break; } }
Copy after login

In this way, an error handling function is customized. So how to hand over error handling to thiscustom function?

// 应用到类 set_error_handler(array(&$this,"appError")); //示例的做法 set_error_handler("my_error_handler");
Copy after login

so easy, in this way, the contradiction between security and debugging convenience can be well solved. And you can also put some thought into making the error message more beautiful to match the style of the website.
The original author gave two points that need attention. I will post them too, hoping to attract the attention of our compatriots:
1. E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, and E_COMPILE_WARNING will not be affected by this. The handle is processed, that is, it will be displayed in the most original way. However, these errors are caused by compilation or PHP kernel errors and will not occur under normal circumstances.
2. After using set_error_handler(),error_reporting() will be invalid. That is, all errors (except the above errors) will be handed over to the custom function for processing.
Finally, give an example

//先定义一个函数,也可以定义在其他的文件中,再用require()调用 function myErrorHandler($errno, $errstr, $errfile, $errline) {      //为了安全起见,不暴露出真实物理路径,下面两行过滤实际路径 $errfile=str_replace(getcwd(),"",$errfile); $errstr=str_replace(getcwd(),"",$errstr); switch ($errno) { case E_USER_ERROR: echo "My ERROR [$errno] $errstr
\n"; echo " Fatal error on line $errline in file $errfile"; echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")
\n"; echo "Aborting...
\n"; exit(1); break; case E_USER_WARNING: echo "My WARNING [$errno] $errstr
\n"; break; case E_USER_NOTICE: echo "My NOTICE [$errno] $errstr
\n"; break; default: echo "Unknown error type: [$errno] $errstr
\n"; break; } /* Don't execute PHP internal error handler */ return true; } //下面开始连接MYSQL服务器,我们故意指定MYSQL端口为3333,实际为3306。 $link_id=@mysql_pconnect("localhost:3333","root","password"); set_error_handler(myErrorHandler); if (!$link_id) { trigger_error("出错了", E_USER_ERROR); }
Copy after login

The above is the detailed content of Detailed explanation of the use of php set_error_handler() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!