Location: PHPKode > scripts > User Friendly Error Pages > user-friendly-error-pages/error.php
<?PHP
/**
 * error.php - User Friendly Error Pages
 *
 * Used in conjunction with .htaccess files, UF Error Pages
 * tells a user what went wrong and gives them options to 
 * better navigate your site.
 *
 * @author Edward Ritter <hide@address.com>
 * @version $Id: error.php,v 1.3 2002/08/23 18:17:01 esritter Exp $
 * @module error
 * @modulegroup addons
 * @package phpWebSite
 */

// edit these variables below
$index = 0;  // set to 2 to show right blocks


/**
  * This script is capable of mailing the details of each 404 error
  * to the webmaster. Use the $reportlevel variable to control when
  * you receive these reports.
  *
  * 0 = don't use the email capabilities at all
  * 1 = send email only if the error's referer contains your domain name
  *     (i.e. the 404 was generated by a broken link on your site)
  * 2 = send email any time a 404 error is generated (useful for tracking
  *     broken links at other sites which link to you)
  */
$reportlevel = 0;

// stop editting here

// include core system files
if (!isset ($mainfile)) { 
    include ('mainfile.php'); 
    include ('config.php');	
}

$req_doc = $phpws_url . $REQUEST_URI;
$domain = ereg_replace ('http://','', $phpws_url);

// pull remaining config vars from flags table
$result = mysql_query ("SELECT top, notify_from FROM flags")
	or die ("Can't open flags table");
if (mysql_num_rows ($result) > 0 ) {
	extract (mysql_fetch_array ($result));
}

/** 
  * topiclist - displays topics for dropdown list
  *
  * @author Edward Ritter
  */
function topiclist() {
	global $table_prefix;
    $toplist = mysql_query ("SELECT topicid, topictext 
    	FROM ".$table_prefix."topics ORDER BY topictext")
    	or die ("Can't open topics table");
    if (mysql_num_rows ($toplist) > 0) {
    	$content .= "<select name=\"topic\" onchange='submit()'>
			<option value=\"\">All Topics</option>\n";
    	while (@extract (mysql_fetch_array ($toplist))) {
			if ($topicid==$topic) { $sel = "selected "; }
			$content .="<option $sel value=\"$topicid\">$topictext</option>\n";
        	$sel = "";
    	}
    	$content .= "</select>";
    	return $content;
    }
}

/** 
  * print_error - display HTTP error based on code number
  *
  * @author Edward Ritter 
  * @param int code: HTTP error code
  */
function print_error($code) {
    global $req_doc, $reportlevel, $table_prefix, $top, $domain;
    include ('header.php');
    switch ($code) {
	case '401':
	    $boxtitle = "401 - Unauthorized";
		$boxstuff = "We're sorry. You are not authorized to view, $req_doc";
	    break;
	case '403':
	    $boxtitle = "403 - Forbidden";
	    $boxstuff = "The page (or directory) you requested was forbidden.
			This is probably because the directory you tried to get was a 
			private directory.";
	    break;
	case '404':
	    $boxtitle = "404 Error Message: Page Not Found";
	    $boxstuff = "We're sorry. The page you requested, <b>$req_doc</b>, 
			doesn't exist on $sitename </p>";
	    if ($reportlevel != 0) {
			$boxstuff .="<p><b>The details of this error have automatically 
				been mailed to the webmaster.</b></p>";
		}
	    $boxstuff .= " <h2>Common Mistakes</h2>
    		The most common mistakes in accessing $sitename are:
    		<ul><li>Making the URL end in <code>.htm</code> - <b>
    		all pages on $sitename  end in <code>.php</code></b></li>
            <li>Using UPPER CASE CHARACTERS - <b>all names are in lower 
            case only</b></li></ul>
			<h2>Most Popular Pages</h2>
    		You may have been trying to reach one of these pages:<ul>";
		$count=1;
		$result = mysql_query ("SELECT sid, title, time, counter 
			FROM ".$table_prefix."stories ORDER BY counter DESC LIMIT 0,$top")
			or die ("Can't open stories table");
		if (mysql_num_rows ($result) > 0) {
			while (@extract (mysql_fetch_array ($result))) {
				if ($counter>0) {
					$boxstuff .= "<li>$count: <a href=\"/article.php?sid=$sid\">$title</a> 
						- read: $counter times<br/>";
					$count++;
				}
			}
		}
		mysql_free_result ($result);
		$boxstuff .= "</ul><h2>Try the Home Page</h2>
			Or you can simply try to start from the <a href=\"/\">$sitename 
			home page</a>.<h2>Search $sitename</h2><p>
			Final option: Use our site search engine to locate the 
			information you want. You may search $sitename by: </p>
			<div align=\"center\"><table border=\"0\" width=\"70%\"><tr>
			<form action=\"search.php\" method=\"post\"><td><b>Keyword</b>
			<input type=\"name\" name=\"query\" size=\"15\"/></td></form>
			<form action=\"search.php\" method=\"get\"><td>
			<b>or Topic: </b>".topiclist()."</font></td></form></tr>
			</table></div>";
	    break;
	case '500';
	    $boxtitle = "500 - Internal Server Error";
	    $boxstuff = "Sorry, it appears that we've misconfigured a script
        	( $req_doc ) somehow. We apologize for the inconvenience.";
	    break;
	default:
	    break;
    }
    thememainbox($boxtitle,$boxstuff);
    include ('footer.php');
}

/**
  * send_email - sends error 404 details to the webmaster
  *
  * @author Edward Ritter
  */ 
function send_email() {
    // Request access to the global variables we need
    global $config, $HTTP_REFERER, $adminmail, $REMOTE_ADDR, $req_doc, $notify_from, $PHP_SELF;
	$errortime = date ("m/d/Y \a\t h:i");

    // Create the body of the email message
    $message .= "404 Error Report\n\nA 404 error was encountered by 
    	$REMOTE_ADDR on $errortime.\n\n
		The URI which generated the error is: \n$req_doc\n\n
		The referring page was:\n$HTTP_REFERER\n\n";

    // Send the mail message. This assumes mail() will work on your system!
    mail ("$adminmail", "404 Error Report", $message, "From: $notify_from");
    return;
}

// Done with function declarations. Main function begins here.
switch ($error) {
    case '401':
		print_error('401');
		break;
    case '403':
		print_error('403');
		break;
    case '404':
		print_error('404');
		break;
    case '500':
		print_error('500');
		break;
    default:
		break;
}

// See whether or not we should send an email report. If so, do it.
if ($reportlevel != 0) {
	if ($reportlevel == 1) {              
		if (eregi ($domain,$HTTP_REFERER)) {
			send_email(); 
		} else {
			send_email();
		}
	}
}

// All done!
exit;

?>
Return current item: User Friendly Error Pages