PHP automatic page capture 500 error example

Source: Internet
Author: User
Tags php error
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:

  1. Array
  2. (
  3. [Type] => 8
  4. [Message] => Undefined variable: http://www.phpfensi.com
  5. [File] => C: WWWindex. php
  6. [Line] => 2
  7. )

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:

  1. Error_reporting (E_ALL); // E_ALL
  2.  
  3. Function cache_shutdown_error (){
  4.  
  5. $ _ Error = error_get_last ();
  6.  
  7. If ($ _ error & in_array ($ _ error ['type'], array (1, 4, 16, 64,256,409 6, E_ALL ))){
  8.  
  9. Echo 'Your code has an error:
    ';
  10. Echo 'fatal error: '. $ _ error ['message'].'
    ';
  11. Echo 'File: '. $ _ error ['file'].'
    ';
  12. Echo 'in line'. $ _ error ['line '].'
    ';
  13. }
  14. }
  15. 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:

  1. // Disable error reporting
  2. Error_reporting (0 );
  3. // Report running errors
  4. Error_reporting (E_ERROR | E_WARNING | E_PARSE );
  5. // Report all errors
  6. Error_reporting (E_ALL );
  7. ?>

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.