On one hand, you want pages that are dead simple: some text, an image or two, and static content. Nothing can go wrong, which means your users get some level of reassurance and comfort. On the other hand, the error page in Figure 8-5 is awfully generic. It just doesn’t S -I/very much. It would be nice to see something about what actually went wrong, maybe like the following:
The result, Figure 8-6 seems to be a good compromise between a truly generic error page and one that is so tricked-out with user-specific information that it becomes prone to error itself.
In the next section, you’ll put this personalized error message in place and still keep the programming minimal.
Creating a PHP Error Page
Almost everything on your template is straight HTML. The only thing that’s dynamic that would change from request to request-is the error message, so your task is relatively simple from a programming standpoint. Begin by putting in a variable for the error message; you’ll come back and assign a value to that variable a little later.
Save this file as show_error.php . But, before you do, keep in mind that this error page is for all your scripts and HTML pages.
Next, you need to get the error message. The least error-prone way to do that is by using request parameters and the $ REQUEST array.
Save this file as show_error.php. But, before you do, keep in mind that this error page is for all your scripts and HTML pages.
What’s so good about this approach? First, it’s about as basic as your PHP programming can be. You’re not using a calculation, per se, you’re just pulling a value out of an array, instead. Better still, it’s not your own custom array, but one that PHP provides for you, and even fills for you, using information supplied in the request to show_error.php.
Testing Your Solution
With your request parameter now in place, it’s time to test it out in a browser. Visit your script’s URL and add a request parameter. For example, you might use something like this in your URL:
You should see something like Figure 8-7, which is a nice-looking error page that didn’t take lot of work to produce.
This simplicity-using request parameters that are just plain text. passed from one page or script to another-is the beauty of show_error.php. There’s very little that can go wrong. That’s what you want in an error page: elegance and simplicity.
You do need to make one fix, though: that backslash showing up before a single apostrophe (“There\’ s” in the first sentence) is no good. You can get rid of that with a little regular expression magic. Replace all occurrences of a backslash with …well, with nothing:
$error _message = preg_replace(” /\\ \\/”,
PHP has an oddity in that you need to actually use four back slashes to match a single backslash. So, \ \ \ \ matches \. oddly enough. That’s because you’re sort of “fighting” the PHP escape mechanism-which uses a backslash (For a refresher see the’ box on page 158).
Expect the Unexpected
Things are looking good. But once again, you’re assuming that things go just the way you want. In fact, that’s exactly the sort of thinking that leads people to ignore error pages. If you need to deal with problems to the point that you’re Creating an error page, you’d better believe that problems can also occur when you’re actually . the error page.
Thankfully, you’ve cut down on most of that by keeping your error page simple (read: less error prone). But what if there’s no error_message request parameter? In that case, you get something like Figure 8-8.
You’re back to instilling your visitors with possible confusion, and that’s no good. There’s an easy solution, though: just deal with the situation when there’s no request parameter:
Go ahead and set the $error_message-variable. If there’s not a request parameter for error_message, set the $error _message variable to a conversational, albeit generic, message. is set returns true if a variable has been assigned something and is not null. Go to your error page again, without anything on the URL, and you’ll get a nice looking page once again. Check out Figure 8-9 for what you should expect.