function find_my_page() { // get the name of the 'missing' file $page = basename($_SERVER['REDIRECT_URL']); // used to pick the correct dictionary $dir = dirname($_SERVER['REDIRECT_URL']); $key = md5($dir); // load spelling dictionaries $ps = pspell_config_create("en"); pspell_config_personal($ps, "./{$key}.pws"); $pl = pspell_new_config($ps); // find alternatives $alt = pspell_suggest($pl, $page); if (!$alt) { // no matches, no choice but to show site map display_site_map(); return; } // escape data for sqlite foreach ($alt as $key => $file) { $alt[$key] = sqlite_escape_string($file); } // fetch all matching pages; $db = new sqlite_db("./typos.sqlite"); $alt = $db->single_query("SELECT url FROM typo WHERE key IN('" . implode("','", $alt) . "')"); switch (@count($alt)) { case 1: // if only one suggestion is avaliable redirect the user to that page header("Location: {$alt[0]}"); return; break; case 0: // no matches, no choice but to show site map display_site_map(); break; default: // show the user possible alternatives if >1 is found echo "The page you requested, '{$_SERVER['REDIRECT_URL']}' cannot be found.<br />\nDid you mean:\n"; foreach ($alt as $url) { echo " <a href='{$url}'>{$url}</a><br />\n"; } } }
// determine the extension of the not found page. $extension = strtolower(substr(strrchr($_SERVER['REDIRECT_URL'], '.'), 1)); // actual error handling switch ($extension) { case 'php': case 'cgi': four_oh_four_handler('DYNAMIC', 1); // Display a site map to the user so that // they can find the page they are looking for. display_site_map(); break; case 'html': case 'htm': case 'shtml': four_oh_four_handler('STATIC', 2); display_site_map(); break; case 'jpg': case 'gif': case 'png': four_oh_four_handler('IMAGE', 3); // to prevent ugly broken image // redirect the request to a blank pixel image. header("Location: /blank.gif"); return; break; default: four_oh_four_handler('UNKNOWN', 4); display_site_map(); break; }