In this article, I attempt to catch a menu choice error quickly and redirect to a common error page template. Despite this being my preferred route I was dissatisfied with its execution. I later abandoned this code for a simpler option. Nonetheless, I think it is instructive showing you attempts that fail. Very oddly, I now think I know how this might be accomplished. However, I am limiting myself here to tested methods; I may get to it later. Until then this and the next article desmonstrates tested approaches including their misdirections.
I am going to repeat the code in concise form that was presented in the Page Failure Notification, which is catching failure to match the passed $_GET value to an extant menu choice. First here is the a reminder how the value is sent to connect to the correct menu contents:
http://[site]/[path]/[menu-template].[ext]?item=[code]
we left off in the html head(er)tag area where we had just completed emailing the maintainer(s) of the menu failure. I will remind you the error has been caught and the variable, $if_error, has been set to true:
<head>
<BASE HREF="http://www.example.com/">
<?php
$get_it = urldecode($_GET["item"]);
$menu_meta = $_SERVER['DOCUMENT_ROOT']."/[subdirectories]/";
$all_menus = array(tags,template,hardware,problems,tools,energy);
$if_error = false;
/* test to see if $get_it matches possible menus selection. */
if (in_array($get_it, $all_menus)) {
/* insert specific meta tags for this menu here. */
readfile($menu_meta.$get_it.".txt");
} else {
$if_error = true;
/* Warn responsible parties of failure via email */
[Emails were sent, look at previous article for details.]
// New code begins here:
echo '<meta http-equiv="Refresh" content="5;
url=http://emample.com/[path]/[error-page].[ext.]">';
...
</head>
note key attribute http-equiv for redirction to the new page listed after the "url=http://..." and "content=5" is the delay in seconds until the command is run. I would advise not taking the delay literally nor should you take the advice listed in the footnote [1.], below as the last words on the topic [2.]. In my hands I reached my redirect page, however, not without significant problems, which I will cover further.
I think it will be too hard for most to read the message sent to the user in the small screen shot displayed below this section. This qouted message comes from a stored text file on my source control system. The message appears to be an exact match:
<h1>Menu Error</h1>
<h2>Sorry, a system error has occurred, making
it impossible to generate the menu you selected.</h2>
<h3>The menu you selected is unknown due to a system fault.
While the web master will be notified, we would appreciate
your taking the time to send a message stating what menu
you were trying to reach. The flawed code may not be
interpretable without your aid, thanks. Please hit the
email link just beneath this text:<br /><br /></h3>
<h3>Contact: <a href="mailto:webmaster@bst-softwaredevs.com">
Web Master</a> Thank you for your help.</h3>
The only missing component of the message is the "Return to Home" button that resides in a html form I intend to propagate around most of the pages on the site.
I created a separate error form that I hoped would evolve into a stardard for this site. However, at the stage I worked up the template I was in a rush to prove the error catching code functioned. Therefore, I stinted my efforts regarding graphic refinement.
Here is a reduced sized image of the web page that would be presented to a user that encounters a failure of the $_GET variable to match a known menu type:
Figure 1. Rough Standard Error Form
at your first sight this might appear to have been successful. However, I did not attempt to capture the image that appeared and vanished in a few seconds prior [3.] to the one shown above. Despite reducing the wait to zero, the menu form appeared with a system error telling the user of a script syntax error. Prior to trying the zero wait I searched for a wait function that would have stopped further reading and processing of the page code while allowing the redirect to proceed at its own pace. The nearest function I found was sleep that stopped all action including the redirect. The syntax error message in the menu form page would appear and it was fired by code far below where the redirect was supposedly initiated and in action. One explanation that the server was too slow, however, I suspect it was simply the choice of incorrect, non-optimal code.
I deem the redirect unsatisfactory with the code shown. The ephermal presence of an obvious error message that most likely is uninterpretable to most casual users of the web too distracting and confusing. Instead I decided to stay with the web form and place the error message in the menu template where the article listing would have appeared. That is what I will show you in the next article in this series. It is much smoother without the messages appearing meant for the developers.
Corrections, suggested extension or comments write: How-To-Guy. If the mailto does not work, use this: hcohen[-At-]bst-softwaredevs.com.
© Herschel Cohen, All Rights Reserved
____________________________________________________________________
1. Here is a sample list on html redirects: Instant Refresh,
I am a bit dubious. Another Instant Refresh, with body tags.
Why?
Imperfect Execution, note dropping redirect on page.
301 Redirect, he says Spammers use redirects, hence,
your site could get banned by not hidding your actions.
Return
2. The Net evolves, like the meta tags, having your site
banned for using redirects is not a certainty. Return
3. This is one of the reasons I expressed skepticism about
the redirect acting instaneously. Return