#native_company# #native_desc#
#native_cta#

404 handler

By Mikko Eerola
on August 26, 2001

Version: 1.2

Type: Full Script

Category: HTTP

License: GNU General Public License

Description: Specify this script as your ErrorDocument 404 in .htaccess. The script will print out the 404 page, *plus* email the details of the error to you. Don’t wait for your visitors to tell you about broken links – let your server tell you!

<?
header("Status: 404 Not Found");
 
# 404.php, 8/10/2000; Revised 08/27/2001 by [email protected].
# See Revision notes below for changes.
 
# This script traps 404 errors and mails a notice to the webmaster.
# Requires PHP 3.0 or newer, and mail capability on your system.
#
# Copyright 2000 [email protected] under the GNU Public License.
# Disclaimer: I wrote this script for me, and it works for me.
# If it doesn't work for you, or makes your server explode, 
# that's life. Please email with questions or bug reports.
#
# ***REVISION 1.2***
# This code was revised by [email protected] 08/27/2001
# Revision Changes:
# 1. Filler text now appears after the error msg and in background colour.
# 2. Added webmaster email address (different from error reporting
#    address).
# 3. Added mailto: link to webmaster.
# 4. Fixed errortime reporting, earlier version displayed hours and minutes
#    with only 1 digit, eg. 2:3 (14:03). Got rid of unnecessary errortime code
#    and used date() function instead.
#
# ***REVISION 1.1***
# This code was revised by [email protected] 06/23/2001
# Revision Changes:
# 1. Added 404 header
# 2. Fixed parsing error in PHP4 by adding a ";" in print_details()
#    at the end of the second line of global variables.


# Set these variables to configure the script:

# Set $domain to your domain name (no www)

$domain = "domain.com";

# Set $docroot to the URL of the directory which contains your
# .htaccess file. Don't include trailing slash.

$docroot = "http://domain.com";

# Font face you'd like to use on the 404 page

$fontface = "Verdana";

# Font size you'd like to use on the 404 page

$fontsize = "2";

# Background color of the 404 page (default is white)

$bgcolor = "#ffffff";

# Text color you'd like to use on the 404 page (default is black)

$textcolor = "#000000";

# 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 = 2;

# Set $emailaddress to the email address of whoever should be
# notified of 404 errors. Don't escape the @ symbol. This will also
# be used as the "from" address on any emails the script generates.
# You can leave this unassigned if you're not using email features.

$emailaddress = "[email protected]";

# If you want the error message to contain a mailto: link to the
# webmaster, set this variable to 1. The email address is specified
# in the variable $webmasteraddress (see below).
#
# 0 = no link
# 1 = display link

$webmasterlink = 1;

# Set $webmasteraddress to the email address you want displayed on
# the error page. Don't escape the @ symbol.

$webmasteraddress = "[email protected]";


################################################################
# DON'T EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING #
################################################################
# If you want to edit the script, I've commented profusely :)  #
################################################################



# The print_details function is what prints the 404 error to
# the visitor. As far as I know, PHP3 doesn't incorporate Perl's
#   print <<"EOT"   ability. PHP4 does allow this capability 
# but the script was written for PHP3. So, you have to use 
# a lot of echo statements if you want to retain PHP3 compat.

function print_details()
  {
   # Request access to the global variables we need
   global $fontface, $fontsize, $docroot, $REQUEST_URI, $reportlevel;
   global $bgcolor, $textcolor, $webmasterlink, $webmasteraddress;

   # Print the 404 error in web format
   echo "<html><head><title>404 Not Found</title></head>";
   echo "<body bgcolor="$bgcolor" text="$textcolor">";
   echo "<b><h1>404 Not Found</h1></b>";
   echo "<p><font face="$fontface" size="$fontsize">";
   echo "<p><font face="$fontface" size="$fontsize">The page you requested, $docroot$REQUEST_URI, doesn't exist";
   echo " on this server.</font></p>";

   # If webmaster shall be displayed, generate html code
   if ($webmasterlink != 0)
     {
      $linktext = "<a href="mailto:" . $webmasteraddress . "">";
      $linkclose = "</a>";
     }

   # If an email report is being generated, let the visitor know:
   if ($reportlevel != 0)
     {
      echo "<p><font face="$fontface" size="$fontsize">";
      echo "The details of this error have automatically been mailed to the " . $linktext . "webmaster" . $linkclose . ".</font></p>";   
     }

   # filler
   echo "<p><font color="$bgcolor" face="$fontface" size="$fontsize">After hours of wondering why IE5 would not display my error message, I found a post on the php.net site that pointed out the page must be larger than 512K for IE 5 to display it rather than IE's lovely "Friendly HTTP error messages."</font></p>";
   echo "<p><font color="$bgcolor" face="$fontface" size="$fontsize">After hours of wondering why IE5 would not display my error message, I found a post on the php.net site that pointed out the page must be larger than 512K for IE 5 to display it rather than IE's lovely "Friendly HTTP error messages."</font></p>";
   echo "<p><font color="$bgcolor" face="$fontface" size="$fontsize">After hours of wondering why IE5 would not display my error message, I found a post on the php.net site that pointed out the page must be larger than 512K for IE 5 to display it rather than IE's lovely "Friendly HTTP error messages."</font></p>";


   # Close up the HTML tags
 #  echo "</body></html>";

   return;
  }


# The send_email function sends the details of the 404 error to the
# webmaster. 

function send_email()
  {
   # Request access to the global variables we need
   global $REQUEST_URI, $HTTP_REFERER, $emailaddress, $REMOTE_ADDR, $docroot;

   # Build the $errortime variable to contain the date/time of the error.
   $errortime = date("D M j Y G:i:s T");

   # Create the body of the email message
   $message .= "404 Error ReportnnA 404 error was encountered by $REMOTE_ADDR";
   $message .= " on $errortime.nn";
   $message .= "The URI which generated the error is: n$docroot$REQUEST_URInn";
   $message .= "The referring page was:n$HTTP_REFERERnn";

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


# Done with function declarations. Main function begins here.

# Send a 404 error to the user's browser
print_details();

# 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;

?>