PHP error handling

Source: Internet
Author: User
Tags php and php error
One of the common errors on the web is invalid links. Once an invalid link appears from another site, you will reschedule your site. People will save their favorite sites as bookmarks. If they visit the site again three months later and only find '2017 Not Fount ', they will Not be given task help, tell 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 marks. You can create common pages to report all errors encountered when processing your pages.

PHP and Apache allow you to create your own error pages, but you need to reconfigure them and compile a small amount of code. 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 to specify a resource for the error code that every user may encounter. Add an ErrorDocument 404/error. php directive to your server configuration. This will redirect the user to 'error. Php' when accessing a page that does not exist. The 'error. Php' page will be written below. 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 and friendly message and 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 set when Apache executes an ErrorDocument directive and 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 very 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 environment variables. In this way, you can use the same error page to handle various errors. The following is an example:
--------------------------------------------------
<? Php
Function throw_error ($ message ){
$ Error_page = "/err/error. php ";
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.