An all to common error on the web is broken links. You end up with broken links from other sites
whenever you rearrange your site. People bookmark some page they like and come back 3 months later
only to find a '404 Not Found', giving them no help whatsoever about where to start looking for the
page you originally had on your site. Let's solve this, or atleast be friendly to your users and
help them get back on track whenever they hit 'a 404'. You can even create a common page to report all
errors you might encounter while rendering your pages.
PHP together with Apache gives you quite alot of freedom to create your own error pages, but requires
some reconfiguring and a tiny bit of coding. Let's start off with the configuration part.
The Apache
ErrorDocument
directive specifies what document (or URI) Apache should redirect a user to in case of an error.
It allows you to specify one resource for each and every error code one of your users might run into.
Start off by adding a
ErrorDocument 404 /error.php directive to your server configuration. This will redirect
users that ask for a page that does not exist to the 'error.php' page you will soon write. Don't
forget to restart Apache for the changes to take effect.
Next, we write up a very simple error.php:
The file you requested (<?=$REDIRECT_URL?>) does not exist on this server.
Please look for the page you wanted at <A HREF="/">the front page</A>.
Now try to access a page that doesn't exist on your server, and voila, you're at the error.php page
with a nice and friendly message and a link to your front page!
Let's extend this. As you can see, I used the REDIRECT_URL variable on the error.php page. This
is a variable that Apache sets whenever it invokes an ErrorDocument directive, and gives you
a possibility to find the originating resource whenever there's an error. Apache also sets a number
of other variables in this case, all of them
documented here. Use these variables to create a nice error page that gives your users
a nice and friendly error page instead of the good ol' boring Apache default page.
Throwing errors from PHP pages
Throwing errors from a PHP page is quite the same as emulating Apache's behaviour for ErrorDocument
directives, you simply redirect the user using a query-string that specifies variables that
Apache usually sets as environment variables. This makes it possible to use the same error page
for all kinds of errors. 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 be able to throw errors anywhere on this page.
if(!condition) {
throw_error("the condition failed");
}
ob_end_flush(); // Page rendered, flush the output buffer.
?>
Using the PHP4 feature called output buffering also helps creating generic error reporting
functionality. By not flushing the output buffer until you are sure the whole page has rendered
error-free, you are able to make Header calls anywhere in your code to redirect the user.
I'll leave up to the reader to design and implement his/her own error.php page to suit his/her site.
Don't forget that you can include a form with email submission possibilities for the users to send
you comments.