PHP error page

Source: Internet
Author: User
Tags php error


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
For every error that your users may encounterCodeSpecify a resource. 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?> ) Does not exist on this server.
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.

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.