home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / 404Handler.php next >
Encoding:
PHP Script  |  2001-10-03  |  4.5 KB  |  141 lines

  1. <?
  2. # 404.php, 8/10/2000. Requires PHP 3.0 or newer.
  3. # Bugfix 6/6/2001 thanks Kishore!
  4. # Traps 404 errors and mails a notice to the webmaster.
  5. # Copyright 2001 shaun@shat.net under the GNU Public License
  6. # Please email with questions or bug reports
  7.  
  8. # Set these variables to configure the script:
  9.  
  10. # Set $domain to your domain name (no www)
  11.  
  12. $domain = "your.domain.com";
  13.  
  14. # Set $docroot to the URL of the your document root.
  15.  
  16. $docroot = "http://your.domain.com";
  17.  
  18. # Font face you'd like to use on the 404 page
  19.  
  20. $fontface = "Verdana";
  21.  
  22. # Font size you'd like to use on the 404 page
  23.  
  24. $fontsize = "2";
  25.  
  26. # Background color of the 404 page (default is white)
  27.  
  28. $bgcolor = "#ffffff";
  29.  
  30. # Text color you'd like to use on the 404 page (default is black)
  31.  
  32. $textcolor = "#000000";
  33.  
  34. # This script is capable of mailing the details of each 404 error
  35. # to the webmaster. Use the $reportlevel variable to control when
  36. # you receive these reports.
  37. #
  38. # 0 = don't use the email capabilities
  39. # 1 = send email only if the error's referer contains your domain name
  40. #     (i.e. the 404 was generated by a broken link on your site)
  41. # 2 = send email any time a 404 error is generated (useful for tracking
  42. #     broken links at other sites which link to you)
  43.  
  44. $reportlevel = 2;
  45.  
  46. # Set $emailaddress to the email address of whoever should be
  47. # notified of 404 errors. Don't escape the @ symbol.
  48. # You can leave this unassigned if you're not using email features.
  49.  
  50. $emailaddress = "you@your.domain.com";
  51.  
  52.  
  53.  
  54. ################################################################
  55. # DON'T EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING #
  56. ################################################################
  57. # If you want to hack the script, I've commented profusely :)  #
  58. ################################################################
  59.  
  60.  
  61.  
  62. # The print_details function is what prints the 404 error to
  63. # the visitor. As far as I know, PHP3 doesn't incorporate Perl's
  64. #   print <<"EOT"   command. PHP4 does allow this capability 
  65. # but the script was written for PHP3. So, you have to use 
  66. # a *lot* of echo statements...
  67.  
  68. function print_details()
  69.   {
  70.    # Request access to the global variables we need
  71.    global $fontface, $fontsize, $docroot, $REQUEST_URI, $reportlevel;
  72.    global $bgcolor, $textcolor;
  73.  
  74.    # Print the 404 error in web format
  75.    echo "<html><head><title>404 Not Found</title></head>";
  76.    echo "<body bgcolor=\"$bgcolor\" text=\"$textcolor\">";
  77.    echo "<b><h1>404 Not Found</h1></b>";
  78.    echo "<p><font face=\"$fontface\" size=\"$fontsize\">";
  79.    echo "We're sorry. The page you requested, $docroot$REQUEST_URI, doesn't exist";
  80.    echo " on this server.</font></p>";
  81.  
  82.    # If an email report is being generated, let the visitor know:
  83.    if ($reportlevel != 0)
  84.      {
  85.       echo "<p><font face=\"$fontface\" size=\"$fontsize\">";
  86.       echo "The details of this error have automatically been mailed to the webmaster.";   
  87.      }
  88.    return;
  89.   }
  90.  
  91.  
  92. # The send_email function sends the details of the 404 error to the
  93. # webmaster. 
  94.  
  95. function send_email()
  96.   {
  97.    # Request access to the global variables we need
  98.    global $REQUEST_URI, $HTTP_REFERER, $emailaddress, $REMOTE_ADDR, $docroot;
  99.  
  100.    # Build the $errortime variable to contain the date/time of the error.
  101.    # Using date() likely would have been better, but I already had this code 
  102.    # elsewhere, and I'm lazy.
  103.    $today = getdate(); 
  104.    $month = $today[mon]; 
  105.    $mday = $today[mday]; 
  106.    $year = $today[year]; 
  107.    $hours = $today[hours];
  108.    $minutes = $today[minutes];
  109.    $errortime = "$month/$mday/$year at $hours:$minutes"; 
  110.  
  111.    # Create the body of the email message
  112.    $message .= "404 Error Report\n\nA 404 error was encountered by $REMOTE_ADDR";
  113.    $message .= " on $errortime.\n\n";
  114.    $message .= "The URI which generated the error is: \n$docroot$REQUEST_URI\n\n";
  115.    $message .= "The referring page was:\n$HTTP_REFERER\n\n";
  116.  
  117.    # Send the mail message. This assumes mail() will work on your system!
  118.    mail("$emailaddress", "404 Error Report", $message, "From: $emailaddress");
  119.    
  120.    return;
  121.   }
  122.  
  123. # Done with function declarations. Main function begins here.
  124.  
  125. # Send a 404 error to the user's browser
  126. print_details();
  127.  
  128. # See whether or not we should send an email report. If so, do it.
  129. if ($reportlevel != 0)                  
  130.   if ($reportlevel == 1) {              
  131.     if (eregi($domain,$HTTP_REFERER))   
  132.       send_email(); }
  133.   else
  134.      send_email();                       
  135.  
  136. # Close up the HTML tags
  137. echo "</body></html>";
  138.  
  139. ?>
  140.  
  141.