PHP Set_error_handler () function use, phpseterrorhandler_php tutorial

Source: Internet
Author: User

Use of PHP Set_error_handler () function, Phpseterrorhandler


When we write a program, there will inevitably be problems (often problems), and when PHP encounters an error, it gives the location of the error script, the number of rows, and the reason. A lot of people say it's not a big deal. Indeed, during the debugger phase, this is really nothing, and I think it is necessary to give the wrong path.

But the consequences of revealing the actual path are disastrous, and for some intruders, this information is very important, and in fact there are a lot of servers that are having this problem. Some network management simply set the PHP configuration file display_errors to off to solve (it seems that we are doing this), but I think this method is too negative.

Sometimes, we really need PHP to return the wrong information for debugging. And in the case of errors may also need to give the user a confession, or even navigate to another page.

So, what's the solution?

Set_error_handler ()

PHP provides the function Set_error_handler () of the custom error handling handle starting from 4.1.0, but very few script writers know. Set_error_handler This function is a good way to prevent false path leaks, of course, there are other more functions.

Set_error_handler is used as follows:

string int Error_types])

Now we are using custom error handling to filter out the actual path. Suppose there is a variable $admin, we are used to determine whether the visitor is an administrator (can make this judgment by IP or login user ID)

//Admin is the administrator's identity, and true is administrator. //The custom error handler 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 it's not an administrator    if(!Admin) {$errfile=str_replace (GETCWD (),"", $errfile); $errstr=str_replace (GETCWD (),"", $ERRSTR); }      Switch($errno) { CaseE_error:echo"ERROR: [ID $errno] $errstr (line: $errline of $errfile) \ n"; Echo"The program has stopped running, please contact the administrator. "; //exit script when error level errors are encountered         Break;  CaseE_warning:echo"WARNING: [ID $errno] $errstr (line: $errline of $errfile) \ n";  Break; default:         //do not show errors at notice level         Break; } }  

So you can customize an error handler, so how do you give the wrong handling to the custom function?

Set_error_handler ( Array (&$ this,"apperror"     Set_error_handler ("my_error_handler");   

So easy, this can be a good solution to the security and debugging convenience contradictions. And you can also take a little thought to make the error message more beautiful to match the style of the website.

The original author gave two points to pay attention to the place, I also let out, hope to arouse the attention of our compatriots:

//define a function, or you can define it in another file, and then use require () to callfunction MyErrorHandler ($errno, $errstr, $errfile, $errline) {//for security reasons, do not expose the real physical path, the following two lines filter the actual path$errfile =str_replace (GETCWD (),"", $errfile); $errstr=str_replace (GETCWD (),"", $ERRSTR); Switch($errno) { CaseE_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; CaseE_user_warning:echo" My WARNING [$errno] $errstr
\ n"; Break; CaseE_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; } //The following begins the connection to the MySQL server, we deliberately specify the MySQL port is 3333, the actual is 3306. $link _id= @mysql_pconnect ("localhost:3333","Root","Password"); Set_error_handler (MyErrorHandler); if(!$link _id) {Trigger_error ("something went wrong.", E_user_error); }

Well, to summarize, here are three ways to use Set_error_handler:

classCallbackclass {function callbackfunction () {//refers to $this} function Staticfunction () {//doesn ' t refer to $this}} function Nonclassfunction ($errno, $errstr, $errfile, $errline) {}//three methods are as follows:  1: Set_error_handler ('nonclassfunction');//go directly to a normal function nonclassfunction  2: Set_error_handler (Array ('Callbackclass','staticfunction'));//go to the static method under the Callbackclass class Staticfunction  3: $o =&NewCallbackclass (); Set_error_handler (Array ($o,'callbackfunction'));//Going to the constructor of a class is essentially the same as the fourth one below.   4. $o =NewCallbackclass (); //The following may also prove useful:  classCallbackclass {function callbackclass () {Set_error_handler (Array (&$ This,'callbackfunction'));//The & is important} function Callbackfunction () {//refers to $this   }  }  

http://www.bkjia.com/PHPjc/1106388.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106388.html techarticle php Set_error_handler () function, Phpseterrorhandler we write the program, inevitably there will be problems (is often encountered problems), and PHP encountered an error, will give a bit of error script ...

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.