SoFunction
Updated on 2025-04-09

PHP error interface


One of all common mistakes on the web is invalid links. Once an invalid link appears from another site, you will reschedule you
site. People will bookmark their favorite sites, and if they visit again three months later, they only find '404 Not Fount'
When they will not be given a 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 and get some traces once they encounter a 'a 404' error. You can create normal pages
Report all errors encountered when processing your page.

PHP and Apache can freely let you create your own error page, but it needs to be reconfigured and a small number of generations are required.
code. Let's learn the configuration part first.

Apache's ErrorDocument indication is used to indicate which document (or URI) Apache should redirect to when an error occurs. It allows you
Specify a resource for every error code your user may encounter. By adding an ErrorDocument 404 to your server configuration
/instruct. This will be redirected to '' when the user visits a page that does not exist, and we will write it
''page. Don't forget to restart Apache for the changes to take effect.

Next, we write a simple one:

The file you requested (<?=$REDIRECT_URL?> ) does not exist on this server.
Please find the file you want from <A HREF="/">Previous page</A>.

Now try to read a page that does not exist on your server, how, you can see that it has a good and
Friendly news, and there is also a link to the previous page.

Let's expand it. As you can see, I'm using the REDIRECT_URL variable in it. This variable is Apache
A ErrorDocument indication is executed and a possibility to find the original resource is given. In this case, Apache
Some other variables are also set, all of which can be found here. Using these variables may create a good error page for
The user is a nice and friendly error page, instead of the default page given by Apache.

Output error from PHP page
Output errors from a PHP page are similar to simulated Apache's ErrorDocument indication, you just simply redirect the user.
By using query-string variables, Apache is usually set inside environment variables. This allows you to use the same error page
Resolve all kinds of mistakes. Here is an example:



<?php

function throw_error($message) {
$error_page = "/err/";

$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 in this page

if(!condition) {
throw_error("the condition failed");
}

ob_end_flush();
// The page is processed and refresh the output buffer
?>
Using PHP4's output buffering feature will also be helpful in generating general error reports. But on the page you confirm the entire error has been processed
When, don't forget to refresh the buffer, you can redirect it through the Header call anywhere in your code.