Analysis of Methods for Handling Custom Errors in PHP

  • 2021-11-13 00:58:31
  • OfStack

In this paper, an example is given to describe the method of PHP custom error handling. Share it for your reference, as follows:

Customize how error reports are handled, bypassing the standard PHP error handlers altogether, so that you can print error reports in your own defined format, or change where they are printed (the standard PHP error report shows where the error occurred). Custom error handling can be considered in the following situations.

★ You can write down the wrong information and find out some problems in the production environment in time.

★ Can be used to screen errors. Errors will reveal some information to users, which is very likely to become a tool for hackers to attack your website.

★ You can do corresponding processing, put all error reports in the final output of the script, or jump to the predefined error page when an error occurs, providing a better user experience. If necessary, you can also terminate the script operation in the custom error handler according to the situation.

Can be used as a debugging tool, 1 some time must debug 1 something while running the environment, but do not want to affect the users who are using it.

Usually used set_error_handler() Function to set user-defined error handling function, this function is used to create the user's own error handling method during runtime, return the old error handler, if failed, return null. This function has two arguments, the first of which is required, requires a callback function, and specifies the function to run when an error occurs. This callback function 1 must declare four arguments, otherwise it is invalid, which are "whether there is an error", "error message", "error file" and "error line number" in order. set_error_handler() The second parameter of the function is optional, specifying which error reporting level now displays user-defined errors. The default is "E_ALL". An example of custom error handling is as follows:


<?php
error_reporting(0); // Errors in masking programs 
// Definition Error_Handler Function, as the set_error_handler() The first of a function 1 Parameter "callback" 
function error_handler($error_level,$error_message,$file,$line){
$EXIT =FALSE;
switch($error_level){
// Reminder level 
case E_NOTICE:
case E_USER_NOTICE:
$error_type = 'Notice';
break;
// Warning level 
case E_WARNING:
case E_USER_WARNING:
$error_type='warning';
break;
// Error level 
case E_ERROR:
case E_USER_ERROR:
$error_type='Fatal Error';
$EXIT = TRUE;
break;
// Other unknown errors 
default:
$error_type='Unknown';
$EXIT = TRUE;
break;
}
// Print error messages directly, or write files and databases. Anyway, the error messages are here, and you can send them. 
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 the error affects the normal execution of the program, jump to the friendly error prompt page 
if (TURE==$EXIT){
echo '<script>location = "err.html";</scrpit>';
}
}
// This is the key point, and the error handling is handed over to error_handle()
set_error_handler('error_handler');
// Use undefined variables to report notice Adj. 
echo $novar;
// Divide by 0 To report a warning 
echo 3/0;
// Customize 1 Errors 
trigger_error('Trigger a fatal error',E_USER_ERROR);
?>

In this example, all printed error reports are output according to their own defined format, but there is one point, which can't be captured by the system directly reporting Fatal Error, because the system can't teach you to deal with such a major error. This kind of error must be solved, so the system will directly terminate the program. Use set_error_handler() Function can solve the contradiction between security and debugging convenience, and you can also take a little effort to make error prompts 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, E_COMPILE_WARNING will not be processed by this handle, that is, will be displayed in the most primitive way. However, these errors are compiled or PHP kernel errors, which do not occur under normal circumstances.

② Use set_error_handler() After that, error_reporting() Will be invalidated. That is, all errors (except the above errors) will be taught to handle by their own defined functions.

For more readers interested in PHP related content, please check the topics on this site: "Summary of PHP Error and Exception Handling Methods", "Summary of php String (string) Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Introduction to php Object-Oriented Programming", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: