PHP blocking _php Tutorials for all kinds of exceptions and error blocking methods and fatal errors

Source: Internet
Author: User
Tags deprecated php class php error

PHP blocking methods for all kinds of exceptions and errors and alarms when fatal errors occur


In daily development, most people do this by turning on debug mode in the development environment and turning off debug mode in the product environment. At the time of development, you can look at various errors, exceptions, but on-line to turn off the error display.

The above situation seems very scientific, some people explain that it is safe, others can not see the error, so as not to divulge important information ...

But you have not encountered this situation, the line is good, a line but can not run up and can't find the reason ...

A script, ran for a long time, has no problem, one day suddenly interrupted, and then there is no record is not created what reason ...

Online a payment, others clearly paid, but we did not record, and ourselves to experiment, but is good ...

All of these are due to the fact that the error message has been closed and errors and anomalies have not been logged, which makes it difficult to trace the errors that occur randomly. So the contradiction is coming, that is, do not show errors, but also to track errors, how this is achieved?

The above problems can be achieved by PHP error, anomaly mechanism and its built-in function ' Set_exception_handler ', ' set_error_handler ', ' register_shutdown_function '.

The ' Set_exception_handler ' function is used to intercept a variety of uncaught exceptions and then handle them in a user-defined way

The ' Set_error_handler ' function intercepts various errors and then gives the user a customized way to handle them.

The ' register_shutdown_function ' function is a function called at the end of the php script, with ' error_get_last ' to get the last fatal error

This idea is basically the wrong, abnormal, fatal error interception down, to our custom method for processing, we identify these errors, whether the exception is fatal, if the database or file system is logged, and then use the script to scan these logs, found a serious error immediately send an email or send a text message for the alarm

First we define the error interception class, which is used to intercept errors, exceptions, and handle them in our own defined processing mode, which is placed in the file named ' errorHandler.class.php ', the code is as follows

/** * File name: baseErrorHandler.class.php * Abstract: Error blocker parent class */require ' errorHandlerException.class.php ';//Exception class errorhandler{Public $argvs = Array (), public $memoryReserveSize = 262144;//Spare memory size private $_memoryreserve;//spare memory/** *  Method: Register custom error, exception interceptor * parameter: void * return: void */Public Function Register () {ini_set (' display_errors ', 0); Set_exception_handler (Array ($this, ' handleexception '));//Intercept uncaught exception Set_error_handler (Array ($this, ' handleError ')) ;//intercept various errors here must not swap location//left spare memory for subsequent interception fatal error use $this->memoryreservesize > 0 && $this->_memoryreserve = str_re  Peat (' x ', $this->memoryreservesize); Register_shutdown_function (Array ($this, ' handlefatalerror '));//Intercept fatal Error}/** * Method: Cancel custom error, exception blocker * parameter: void * return: VO  ID */Public Function unregister () {Restore_error_handler (); Restore_exception_handler (); }/** * Method: Handle intercepted uncaught exception * parameter: Exception $exception * return: void */Public Function handleexception ($exception) {$th  Is->unregister (); try {$this->logexcEption ($exception);  Exit (1);  } catch (Exception $e) {exit (1); }}/** * method: Handling intercepted Error * parameter: int $code error code * parameter: String $message error message * parameter: string $file error file * parameter: int $line Wrong number of rows * returned: Boolean/Public Function HandleError ($code, $message, $file, $line) {//The idea is to turn the error into an exception throw uniform to the exception handler for processing I F ((Error_reporting () & $code) &&!in_array ($code, Array (E_notice, e_warning, E_user_notice, e_user_warning , e_deprecated)) {///Here only serious errors are recorded for various warning notice not processed $exception = new Errorhandlerexception ($message, $code, $code, $   file, $line);   $trace = Debug_backtrace (Debug_backtrace_ignore_args); Array_shift ($trace); the first element of//trace removes a foreach ($trace as $frame) {if ($frame [' function '] = = ' __tostring ') for the current object {     If an error occurs in the __tostring method, no exception is thrown $this->handleexception ($exception);    Exit (1);  }} throw $exception; } return false; }/** * Method: Interception of fatal error * parameter: void * return: void */Public Function Handlefatalerror () {unset ($this->_memoryreserve);//release memory for use by the following handler $error = Error_get_last ();//Last error message if (Errorhandlerexception::isfatalerror ($error)) {// If a fatal error is processed $exception = new Errorhandlerexception ($error [' message '], $error [' type '], $error [' type '], $error [' file '],   $error [' line ']);   $this->logexception ($exception);  Exit (1);  }}/** * method: Gets the server IP * parameter: void * Returns: String */Final Public Function Getserverip () {$serverIp = ';  if (Isset ($_server[' server_addr ')) {$serverIp = $_server[' server_addr '];  } elseif (Isset ($_server[' local_addr ')) {$serverIp = $_server[' local_addr '];  } elseif (Isset ($_server[' HOSTNAME ')) {$serverIp = gethostbyname ($_server[' HOSTNAME ']);  } else {$serverIp = getenv (' server_addr ');  } return $serverIp;  }/** * Method: Gets the current URI information * parameter: void * Returns: string $url */Public Function Getcurrenturi () {$uri = ';  if ($_server ["REMOTE_ADDR"]) {//browser browse mode $uri = ' http://'. $_server[' server_name ']. $_server[' Request_uri '; } else {//command-line mode $params = $this-&Gt;argvs;   $uri = $params [0];   Array_shift ($params);   for ($i = 0, $len = count ($params), $i < $len, $i + +) {$uri. = '. $params [$i]; }} return $uri; }/** * Method: Log Exception information * parameter: errorhandlerexception $e Error Exception * Return: Boolean is saved successfully */FINAL public function logexception ($ E) {$error = array (' add_time ' = + Time (), ' title ' = = Errorhandlerexception::getname ($e->getcode () ),//Here Gets the user-friendly name ' message ' + = Array (), ' server_ip ' = $this->getserverip (), ' Code ' = Erro   Rhandlerexception::getlocalcode ($e->getcode ()),//A number is defined here for various errors in order to find ' file ' = = $e->getfile (), ' line '  = = $e->getline (), ' url ' = = $this->getcurrenturi (),); do {//$e->getfile (). ':' . $e->getline (). ' ' . $e->getmessage (). ' ('. $e->getcode ().   ') ' $message = (string) $e;  $error [' message '] [] = $message;  } while ($e = $e->getprevious ());  $error [' message '] = Implode ("\ r \ n", $error [' message ']); $tHis->logerror ($error);         }/** * Method: Log Exception information * parameter: Array $error = Array (* ' time ' = = int, * ' title ' = ' String ', * ' Message ' = ' string ', * ' code ' = = int, * ' server_ip ' = ' string ' * ' file ' =& Gt  ' String ', * ' line ' = + int, * ' url ' = = ' string ', *); * Returns: Whether Boolean is saved successfully */Public function LogError ($error) {/* Here to implement how to log error messages to the journal */}}

In the preceding code, there is a ' errorhandlerexception ' class, which is placed in the file ' errorHandlerException.class.php ', which is used to convert an error to an exception in order to log the file, line number, error code, Information such as an error message, and the method ' Isfatalerror ' is used to identify whether the error is a fatal error. Here we have numbered and named errors for ease of administration. The code for this class is as follows

/** * File name: errorHandlerException.class.php * Abstract: Custom Error Exception class This class inherits from the PHP built-in error exception class */class Errorhandlerexception extends errorexception{public static $localCode = Array (e_compile_error = 4001, e_compile_warning = 40 E_core_error = 4003, e_core_warning = 4004, e_deprecated = 4005, E_err          OR = = 4006, E_notice = 4007, E_parse = 4008, E_recoverable_error = 4009, E_strict = 4010, e_user_deprecated = 4011, E_user_error = 4012, E_user_notice =&G T 4013, e_user_warning = 4014, e_warning = 4015, 4016 = 4016,); public static $localName = Array (e_compile_error = ' PHP COMPILE ERROR ', e_compile_warning = ' PH          P Compile Warning ', e_core_error = ' php core ERROR ', e_core_warning = ' php core Warning ', e_deprecated = ' PHP DEPRECATED Warning ', e_error = ' php Fatal ERROR ', E_notice = ' php NOTICE ', e_parse = ' php PARSE Er Ror ', e_recoverable_error = ' php recoverable ERROR ', e_strict = ' php STRICT Warning ', E  _user_deprecated = ' php user DEPRECATED Warning ', e_user_error = ' php user ERROR ', E_user_notice          = ' php user Notice ', e_user_warning = ' php user WARNING ', e_warning = ' php WARNING ', 4016 = ' Customer ' s Error ', '); /** * Method: Constructor * Summary: Related knowledge See http://php.net/manual/en/errorexception.construct.php * * Parameter: String $message exception information (   Optional) * int $code exception code (optional) * int $severity * String $filename exception file (optional) * int $line number of lines of exception (optional) * Exception $previous Previous exception (optional) * * return: void */Public function __construct ($message = ", $code = 0, $severity = 1, $ filename = __file__, $line = __line__, Exception $previous = null) {parent::__construct ($message, $Code, $severity, $filename, $line, $previous); }/** * Method: Fatal Error * parameter: Array $error * return: Boolean/public static function Isfatalerror ($error) {$fatalError         s = Array (E_error, E_parse, E_core_error, e_core_warning, E_compile_error,  e_compile_warning); return isset ($error [' type ']) && in_array ($error [' type '], $fatalErrors);   }/** * Method: Get local error code according to the original error code * parameter: int $code * return: int $localCode */public static function Getlocalcode ($code) { Return Isset (self:: $localCode [$code])? Self:: $localCode [$code]: Self:: $localCode [4016]; }/** * Method: Gets the user-friendly name from the original error code * parameter: Int * return: string $name */public static function GetName ($code) {return ISS ET (self:: $localName [$code])? Self:: $localName [$code]: Self:: $localName [4016]; }

In the error interception class, the user is required to define the method of implementing the Error Record (' Logexception '), this place needs to be aware that some errors may occur over time, so you can only record once, you may use the error code, file, line number, error details Generates a MD5 value that records whether the error has been recorded and does not need to be recorded if it has been recorded within the specified time (one hours)

Then we define a file to instantiate the above class, catch various errors, exceptions, the file is named ' registererrorhandler.php ', as in the following

/** How to use: * Introduce the file at the entrance, then you can define the debug mode constants in the file ' debug_error ' * * * <?php* * require ' registererrorhandler.php '; * *? >*//* * * Debug Error mode: * 0    =   non-debug mode, do not display exceptions, error messages but log exceptions, error messages * 1    =   Debug mode, display exceptions, error messages but do not log exceptions, error messages */define (' Debug_ Error ', 0); require ' errorHandler.class.php '; class registererrorhandler{/**  * Square  Method: Registration exception, error intercept  * parameter  Number: void  * return  : void *  /public static function register () {  global $argv;  if (debug_error)  {//If you turn on debug mode   ini_set (' display_errors ', 1);   return;  }  If debug mode is not turned on  ini_set (' error_reporting ',-1);  Ini_set (' Display_errors ', 0);  $handler = new ErrorHandler ();  $handler->argvs = $argv;//Get parameters in the main compatible command-line mode here  

All that is left is for you to introduce the file in your portal file, define the debug mode, and then implement your own method of documenting the error.

It is important to note that some errors have occurred before you register and cause the script break to be logged because the ' registererrorhandler::register () ' has not been executed yet.

There is also the ' set_error_handler ' function that does not capture the following types of errors E_error, E_parse, E_core_error, e_core_warning, E_compile_error, E_compile_ WARNING, this can be seen in the official documentation, but this place is no harm, because the above errors are parsing, compiling errors, these are not passed, you are not likely to publish on-line

Articles you may be interested in:

    • How to set PHP error levels
    • Explanation of how to close the PHP error prompt
    • Analysis of various interceptor usages in PHP class
    • Example analysis of a PHP interceptor
    • Example of modifying a PHP script to make WordPress block spam comments

http://www.bkjia.com/PHPjc/1093705.html www.bkjia.com true http://www.bkjia.com/PHPjc/1093705.html techarticle PHP various exceptions and errors of the interception method and the occurrence of fatal error alarm, PHP interception in the daily development, most people's practice is in the development of the environment when the debug mode, in the product ring ...

  • Related Article

    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.