The Set_error_handler () function sets the user-defined error-handling function. This function is used to create the user's own error handling for the Run-time period. The function returns the old error handler, or null if it fails.
Let's take a look at some examples.
Set_error_handler ()
PHP provides a functional function of custom error handling handles Set_error_handler () from 4.1.0, but very few scripting people know. Set_error_handler This function is a good way to prevent the wrong path from leaking, and of course, there are other effects.
1. Can be used to screen errors. An error can cause some information to be leaked to the user, which is very likely to become a tool for hackers attacking your site. Secondly let the user feel your level is very frustrated.
2. You can write down the wrong information and find some problems in the production environment in time.
3. Can do the corresponding processing, error can show jump to a predefined error page, provide a better user experience.
4. As a debugging tool, some things must be debugged in the production environment, but do not want to affect the users in use.
5..
The use of Set_error_handler is as follows:
Copy Code code as follows:
View Sourceprint?1 string Set_error_handler (callback Error_Handler [, int error_types])
We use error_reporting () to see the error message includes three parts, error messages, the absolute address of the error file, the number of errors occurred. In fact, there is another type of error. Array ([Type] => 1 [message] => call to undefined method Someclass::somemedthod () [File] =>/home/zhangy/www/aaa a/stasdf.php [Line] => 67, the absolute path of the page is best not to expose to others, otherwise to some people can be called the machine, in order to eliminate this, many people will adopt, Ini_set ("Display_errors", 0); Directly to the wrong message to screen out. This is not convenient, if we want to look at information to do? Every time you look at it, are you going to change the code, or do you want to change the configuration of Apache, in a heavy start?
PHP has function Set_error_handler can solve this problem
Usage is as follows:
Mixed Set_error_handler (callback $error _handler [, int $error _types = E_all | E_strict])
PHP function register_shutdown_function can also solve this problem
Usage is as follows:
int register_shutdown_function (String $func)
Personally feel that the error function is defined by itself, at least three points of benefit,
1, will not show the absolute path of the file, safe
2, even if there is an error message, we can handle the error message, so that users do not see fatal error such things. Better User Experience
3, the project on the line, sometimes, you still have to help users to solve the problem, this time inevitably to modify the code, but we have to let the error message out, and can not let users see, this time, with set_error_handler such a function is very cool.
The individual did a little test.
<?php error_reporting (0);
Register_shutdown_function (' Error_alert ');
function Error_alert () {if (Is_null ($e = Error_get_last ()) = = False) {Set_error_handler (' ErrorHandler ');
if ($e [' type '] = = 1) {trigger_error ("Fatal error", E_user_error);
}elseif ($e [' type '] = = 8) {trigger_error ("notice", e_user_notice);
}elseif ($e [' type '] = = 2) {trigger_error ("warning", e_user_warning);
}else{Trigger_error ("Other", e_user_other);
}else{echo "no error";
} set_error_handler (' ErrorHandler '); Function ErrorHandler ($errno, $errstr, $errfile, $errline, $errcontext) {switch ($errno) {case E_user_error:echo "&L
T;b>my error</b> [$errno] $errstr <br/>n ";
echo "Fatal error on line $errline in file $errfile"; echo ", PHP". Php_version. " (" . Php_os.
") <br/>n";
Break
Case E_user_warning:echo "<b>my warning</b> [$errno] $errstr <br/>n";
echo "Warning on line $errline in file $errfile"; echo ", PHP". Php_version. " (" . PHp_os.
") <br/>n";
Break
Case E_user_notice:echo "<b>my notice</b> [$errno] $errstr <br/>n";
echo "Notice on line $errline in file $errfile"; echo ", PHP". Php_version. " (" . Php_os.
") <br/>n";
Break
Default:echo "Unknown error type: [$errno] $errstr <br/>n";
echo "Warning on line $errline in file $errfile"; echo ", PHP". Php_version. " (" . Php_os.
") <br/>n";
Break
return true;
Class SomeClass {public Function SomeMethod () {}} someclass::somemedthod ();
$a = "ASDF";
foreach ($a as $d) {echo $d; }?>
Now we're going to filter out the actual path with custom error handling. Suppose there is a variable $admin, we are used to determine whether the visitor is an administrator (you can make this decision by IP or by the user ID of the login).
Admin for Administrator's identity, true to administrator.
Custom error-handling functions must have these 4 input variables $errno, $errstr, $errfile, $errline, otherwise invalid.
function My_error_handler ($errno, $errstr, $errfile, $errline)
{
//filter The actual path if
(!admin) {if it is not an administrator
$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 program has stopped running, please contact the administrator. ";
Exits script exit when error level errors are encountered
;
break;
Case e_warning:
Echo ' WARNING: [ID $errno] $errstr (line: $errline of $errfile) n ";
break;
Default:
//Do not show notice-level error break
;
}
This allows you to customize an error-handling function, so how do you give the wrong handling to this custom function?
Apply to Class
Set_error_handler (Array (& $this, "Apperror"));
Example of the practice of
Set_error_handler ("My_error_handler");
So easy, this can be a good way to solve the security and debugging convenience contradictions. And you can also spend some thought, make the error hint more beautiful to match the website style.
In the example above, I turned off the error message and handled the error with my own function, the page above will report fatal error, and we can use ErrorHandler to control and handle it.
Well, to sum up, here are the three uses of Set_error_handler:
PHP code
Class Callbackclass {
function callbackfunction () {
//refers to $this
}
function staticfunction () {
//doesn ' t refer to $this
}
}
function Nonclassfunction ($errno, $errstr, $errfile, $errline) {
}
//Three methods are as follows:
Set_error_handler (' Nonclassfunction '); Go directly to an ordinary function nonclassfunction
Set_error_handler (Array (' Callbackclass ', ' staticfunction '));//Go to The static method under Callbackclass class Staticfunction
$o =& new Callbackclass ();
Set_error_handler (Array ($o, ' callbackfunction ')); Go to the constructor of a class, essentially the same as the fourth below.
. $o = new Callbackclass ();
The following may also prove useful
class Callbackclass {
function Callbackclass () {
Set_error_handler (Array (& $this, ' callbackfunction ')); The & is important
}
function Callbackfunction () {
//refers to $this
}
}
Here's a little space for you to introduce the PHP Set_error_handler () function alone
Definitions and usage
The Set_error_handler () function sets the user-defined error-handling function.
This function is used to create the user's own error handling for the Run-time period.
The function returns the old error handler, or null if it fails.
Grammar
Set_error_handler (Error_function,error_types)
Parameter description
Error_function required. Specify the function to run when an error occurs.
Error_types Optional. Specify which error reporting level will display user-defined errors. The default is "E_all".
Tips and comments
Tip: If you use this function, you will completely bypass the standard PHP error handler, and if necessary, the user-defined error handler must terminate (Die ()) the script.
Note: If an error occurs before the script executes, the custom error handler is not used because the custom program was not registered at that time.
Example
<?php
//error handler function
function Customerror ($errno, $errstr, $errfile, $errline)
{
echo <b>custom error:</b> [$errno] $errstr <br/> ";
echo "Error on line $errline in $errfile <br/>";
echo "Ending Script";
Die ();
}
Set Error handler
Set_error_handler ("Customerror");
$test =2;
Trigger Error
if ($test >1)
{
trigger_error ("A custom error has been triggered");
>
Output:
Custom error: [1024] A custom error has been triggered
Error on line in c:/webfolder/test.php
Ending Script