PHP error handling

Source: Internet
Author: User


One of the common errors on the web is invalid links. Once an invalid link appears from another site, you will reschedule
. People will save their favorite sites as bookmarks. If they visit the site again three months later, they will only find that '2017 Not Fount'
They will not be given task help, telling them how to find the original information from your site. Let's solve this problem, or at least
Give your users a friendly help. Once they encounter the 'A 404 'error, they will be able to get some trace. You can create a normal page
Report all errors encountered when processing your page. PHP and Apache allow you to create your own error page with great freedom, but you need to re-configure the page and compile a few generations.
. Let's first learn about the configuration section.

Apache ErrorDocument indicates the document (or URI) to which Apache redirects when an error occurs ). It allows you
Specify a resource for the error code that your users may encounter. By adding an ErrorDocument 404 in your server configuration
/Error. php instructions. This will be redirected to 'error. php' when the user accesses a page that does not exist. Below we will write
'Error. php' page. Do not forget to restart Apache to make the change take effect.

Next, let's write a simple error. php:



The file you requested (<? = $ REDIRECT_URL?>) This server does not exist.
Find the file you want from the <a href = "/"> previous page </A>.

Now I try to read a page that does not exist on your server. How can I see error. php? It has a good
A friendly message with a link to the previous page.

Let's extend it. As you can see, I used the REDIRECT_URL variable in error. php. This variable is used by Apache in
When an ErrorDocument directive is executed, it provides a possibility to locate the original resource. In this case, Apache
Some other variables are also set. All variables can be found here. Using these variables may create a good error page
The user has a good and friendly error page, instead of the default page provided by Apache.

Output Error from PHP page
Outputting errors from a PHP page is similar to simulating Apache's instructions on ErrorDocument. You just need to simply redirect users,
By using the query-string variable, Apache is usually set in the environment variable. In this way, you can use the same error page
Handle various errors. The following is an example:

----------------------------------------------------------------------
<? Php

Function throw_error ($ message ){
$ Error_page = "/err/error. php ";

$ Error_url = $ error_page;
$ Error_url. = "? REDIRECT_ERROR_NOTES = $ message ";
$ Error_url. = "& REDIRECT_URL =". $ GLOBALS ["PHP_SELF"];
$ Error_url. = "& REDIRECT_REQUEST_METHOD = $ REQUEST_METHOD ";
$ Error_url. = "& REDIRECT_STATUS = 501 ";
Header ("Status: 501 ");
Header ("Location: $ error_url ");
Exit;
}

Ob_start ();
// Use the output buffer to output errors anywhere on this page

If (! Condition ){
Throw_error ("the condition failed ");
}

Ob_end_flush ();
// After page processing is completed, refresh the output buffer

?> ------------------------------------------------------------------
Using the output buffer feature of PHP4 can also help generate common error reports. However, the entire error page has been processed.
Do not forget to refresh the buffer. You can use the Header call to redirect any part of your code.

Readers can design their own error pages to adapt to their/her websites. Don't forget. You can include an email.
Submit the form on the error page, so that the user can give you some feedback.

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.