PHP automatic page capture 500 error example

Source: Internet
Author: User
Tags ini php error

The page is usually blank when a fatal error occurs in the program. It is not difficult to get the error information! It mainly uses two functions:
Error_get_last () gets the last error message. The structure is as follows:

The code is as follows: Copy code


Array
(
[Type] => 8
[Message] => Undefined variable: http://www.111cn.net
[File] => C: WWWindex. php
[Line] => 2
)

Register_shutdown_function () registers a callback function when the script is stopped.
With these two functions, you can monitor fatal errors:

The code is as follows: Copy code

Error_reporting (E_ALL); // E_ALL
 
Function cache_shutdown_error (){
 
$ _ Error = error_get_last ();
 
If ($ _ error & in_array ($ _ error ['type'], array (1, 4, 16, 64,256,409 6, E_ALL ))){
 
Echo '<font color = red> your code has an error: </font> </br> ';
Echo 'fatal error: '. $ _ error ['message'].' </br> ';
Echo 'File: '. $ _ error ['file'].' </br> ';
Echo 'in the'. $ _ error ['line']. 'line </br> ';
    }
}
 
Register_shutdown_function ("cache_shutdown_error ");

Sequence attached local server test method

The following describes three methods for displaying the PHP error message.

I. php. ini configuration

In the php. ini configuration, there are two configuration variables related to this. The two variables and their default values are as follows:

The code is as follows: Copy code

Display_errors = Off
Error_reporting = E_ALL &~ E_NOTICE

The purpose of the display_errors variable is obvious-it tells PHP whether an error is displayed. The default value is Off. The purpose is to display the error prompt:

The code is as follows: Copy code

Display_errors = On

E_ALL: this setting displays all the information from bad code practices to harmless prompts to errors. E_ALL is a little too detailed for the development process because it displays a prompt even if the variable is not initialized, which is a feature of PHP "advanced. Fortunately, the default value of error_reporting is "E_ALL &~ E_NOTICE ", so that only the error and bad code are displayed, and no negative prompt is displayed for the program.

After modifying php. ini, you need to restart Apache to take effect in apache. Of course, if you test the program only in the command line, you do not need this step.


Configuring the php program

The code is as follows: Copy code

<? Php
// Disable error reporting
Error_reporting (0 );
// Report running errors
Error_reporting (E_ERROR | E_WARNING | E_PARSE );
// Report all errors
Error_reporting (E_ALL );
?>

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.