Once again we look at the same critical error that is detected at the menu page start up. However, this time "Local" means no redirection to an external error page. The process is similar with a good portion of the code unaltered. Nonetheless, there are several significant differences, the reporting of the exception is immediate and the message is placed on the running web page template. Regarding the latter, the message resides where the menu listing would have appeared. This results in a smoother execution with less likelihood of confusion.
I am going to paste in the code and reminders quickly, because this was all shown in the previous article, which attempted to employ a standardized error page. The first issue is reminding you again how the data is passed via a $_GET value:
http://[site]/[path]/[menu-template].[ext]?item=[code]
I am including all the code in the head(er) tag area, with one major exception:
<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.]
...
</head>
That is, I took out the code calling the separate error page. The error has been caught but the user error message is now sent from a differing location of the page template. However, I think it would be advisable to exhibit one of my menu pages, since most viewers of these articles enter and leave via direct links remote from my site:
Figure 1. Example Menu Page from B/ST Web Developers Site
I direct your attention to the central, wide column directly beneath the site title [1.]. It is a floating div tag area with the html code of this form:
<div id="central-col-bst">
<?php
if ($if_error==false) {
readfile($menus . "/menu-list-".$get_it.".txt");
//This time [NAME] is the actual menu
// with appropriate links and descriptions.
} else {
$gohome = $_SERVER['DOCUMENT_ROOT']."/[path]";
readfile($menus."/menu-not-known.txt");
readfile($gohome."/home-button.txt");
}
?>
</div> <!-- End of central B/ST column -->
Once again, I wish to draw your attention to the simplicity of the php code. I also assert it works as I will now show. First, I am repeating the error message that will be difficult to read in the image below (this is taken directly from the previous article in this series):
<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>
As I mentioned previously, the Home button at the bottom is a very simple html form containing a submit button [2.], otherwise you have the complete contents. The image below shows the user error message where the menu listing would have appeared had there not been a failure with the passed GET variable:
Figure 2. User Error on Menu Page
I am going to keep this as my default error report form for all the menus, partially due to the assertion that redirects could result in search engine banning my site. I might change my mind later when I automate the display of my articles using a database for the content source and a very limited set of page templates.
I have essentially trapped only one type of potential error where the GET value is somehow defective. Other problems could arise, however, I will use differing code to address the issue where files might have been lost. I hope to have that option as my concluding article for this series.
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. For those interested in more detail, there is a series on
this site found under the tags menu. The details were
covered under div tags in either the 1st or 2nd of this
series. Return
2. A series I am running on LXer.com will have more complex
code shown. The series began with this introductory article.
Return