How to make 404 Error Page (with reporting and logging) us

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to make 404 Error Page (with reporting and logging) us

Post by Tony » Sun Nov 29, 2009 4:07 am

This Custom 404 PHP script will display an error page. It can also send you an email and/or log all the relevant information when there is a 404 error. This is useful for notifying you of bad links to your site and it is also a great debugging tool, notifying you of your own invalid references. You may want to use the email feature selectively rather than leave it on all the time as it could generate a lot of emails if someone keeps making bad requests. The log file feature is handy for quicky checking 404's on your site, you can check the file via FTP or in your browser.

In order to use it you must add the following line to your .htaccess file. DO NOT use the full URL to the error page, use the relative path as done in the example below.

ErrorDocument 404 /404.php

Assuming the script below is named 404.php and placed in your htdocs directory.

Code: Select all

<?php
/*
This script can send an email and/or make an entry in a log file
There are two variables below - one for an email address and one for a log file
Set both vraiables to the values you want to use
If you do not want either an email or log entry, comment out the respective line
For example, if you do not want an email sent, put a // in front of the $emailAddress line - same for the $logFile line
*/
$logFile = $_SERVER['DOCUMENT_ROOT'].'/404.log';   // full path to your log file - you should create an empty starter file if you use logging
$emailAddress = "[email protected]";  // set to your email address
putenv('TZ=EST5EDT');  // set to your time zone
// change nothing below this line
if (isset($emailAddress)) {
  $message = "Time of the error: ".date(" F d h:ia")."\n\n";
  $message .= "Browser: ".$_SERVER['HTTP_USER_AGENT']."\n\n";
  $message .= "Page Requested: ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n\n";
  $message .= "Referer: ".$_SERVER['HTTP_REFERER']."\n\n";
  $message .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\n\n";
  $message .= "Hostname: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])."\n\n";
  mail($emailAddress,"404 Error", $message,"From: Website <>");
}
if (isset($logFile)) {
  $logData = "404||".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."||".$_SERVER['REMOTE_ADDR']."||".date('Y-m-d H:i:s')."\r\n";
  @file_put_contents($logFile,$logData,FILE_APPEND|LOCK_EX);
}
header("HTTP/1.0 404 Not Found");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>404 - request not found</title>
<style type="text/css">
body { margin: 0px; background-color: rgb(60,60,60) }
a:link { font-weight: bold; color: rgb(60,60,180); }
a:hover {color: rgb(180,60,60); }
</style> 
</head>
<body>
<div style="margin: 100px auto; padding: 30px; border: solid 20px rgb(20,20,20); font-size: 16px; color: rgb(60,60,60); width: 550px; background-color: white;">
<span style="font-weight: bold; font-size: 28px;">request not found</span>
<br /><br /><br />(oops)
<br /><br />it appears you were looking for
<br /><span style="font-weight: bold;"><?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?></span>   
<br /><br />however, since you are here, it is clear you did not get what you wanted.
<br /><br />the problem has been reported so any broken links can be found and repaired.
<br /><br />you can click <a href="javascript:history.go(-1)">here</a> to go back to your previous page.
</div>
</body>
</html>
Post Reply

Return to “PHP & MySQL”