If a 500 error occurs locally during program debugging, you can directly open the phpini error to see where the error is. However, we cannot do this on the server, which makes it easy to feel like a website.
If we encounter a 500 error locally during program debugging, we can directly open php. ini errors, but we cannot do this on the server, this makes it easy for people to feel that the website is not formal and may also display the WEB path and related security data of your website to others. how can we handle the 500 error, let's take a look at a method.
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 () to obtain the last error message. The structure is as follows:
- Array
- (
- [Type] => 8
- [Message] => Undefined variable: http://www.phpfensi.com
- [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:
- 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 'Your code has an error:
';
- Echo 'fatal error: '. $ _ error ['message'].'
';
- Echo 'File: '. $ _ error ['file'].'
';
- Echo 'in line'. $ _ error ['line '].'
';
- }
- }
- Register_shutdown_function ("cache_shutdown_error ");
The following describes the 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. below are the two variables and their default values. the code is as follows:
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. Our purpose is to display the error prompt, so: display_errors = On
E_ALL: this setting will display all information from poor coding 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, this 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.
The code for configuring the php program is as follows:
-
- // Disable error reporting
- Error_reporting (0 );
- // Report running errors
- Error_reporting (E_ERROR | E_WARNING | E_PARSE );
- // Report all errors
- Error_reporting (E_ALL );
- ?>