PHP Error Interface _php Basics

Source: Internet
Author: User
Tags error code php error

One of the common errors on the Web is an invalid link. Once an invalid link appears from another site, you will rearrange your
's site. People will save their favorite sites in a book, and if they visit again after three months, only the ' 404 Not Fount ' will be found.
will not give them 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 and get some traces if they encounter a ' a 404 ' error. You can create an ordinary page to
Report all of the errors you have encountered while working on your page.

PHP, together with Apache, allows you to create your own error pages freely, but you need to reconfigure them and make a small number of generations
Code. Let's learn the configuration section first.

Apache's errordocument instructions are used to indicate which document (or URI) Apache should redirect to when an error occurs. It allows you
Specify a resource for each error code that your users may encounter. By adding a errordocument 404 to Your server configuration
/error.php instructions. This will redirect to ' error.php ' when the user accesses a nonexistent page, and we'll write
' error.php ' page. Don't forget to restart Apache for the changes to take effect.

Next, we write a simple error.php:

The file you requested (<?= $REDIRECT _url?>) does not exist on this server.
Please find the file you want from <a href= "/" > front page </A>.

Now try to read a page that doesn't exist on your server, so you can see error.php, it has a good and
Friendly message, and there is also a link to the previous page.

Let's expand it. As you can see, I used the Redirect_url variable in the error.php. This variable is Apache in
A errordocument is set when a directive is executed, and a possibility is given to find the original resource. In this case, Apache
A number of other variables are set, all of which can be found here. Using these variables may create a good error page for
A nice and friendly error page for the user instead of the default page given by Apache.

Output errors from the PHP page
From a PHP page output error and analog Apache to errordocument instructions to do much like, you simply redirect the user,
By using the query-string variable, Apache is usually set in the environment variable. This allows you to use the same error page to place
All kinds of mistakes. Here 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 output buffering to output errors anywhere on this page

if (!condition) {
Throw_error ("The condition Failed");
}

Ob_end_flush ();
Page processing complete, refresh output buffer
?>
Using the PHP4 output buffering feature can also be useful for generating generic error reporting. But after you confirm the entire error page, you're done.
, don't forget to refresh the buffer, and you can use the header to redirect it anywhere in 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.