PHP: 404 error trap and email it to the administrator program

Source: Internet
Author: User
& Lt ;? # 404.php, 8/10/2000. # Traps404errorsandmailsanoticetothewebmaster. # RequiresPHP3.0ornewer, andmailcapabilityonyoursystem. # Copyright2000shaun@shat.netundertheG

# 404.php, 8/10/2000.
# Traps 404 errors and mails a notice to the webmaster.
# Requires PHP 3.0 or newer, and mail capability on your system.
#
# Copyright 2000 shaun@shat.net under the GNU Public License.
# Disclaimer: I wrote this script for me, and it works for me.
# If it doesnt work for you, or makes your server explode,
# Thats life. Please email with questions or bug reports.

# Set these variables to configure the script:

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

$ Domain = "your.domain.com ";

# Set $ docroot to the URL of the directory which contains your
#. Htaccess file. Dont include trailing slash.

$ Docroot = "http://your.domain.com ";

# Font face youd like to use on the 404 page

$ Fontface = "Verdana ";

# Font size youd like to use on the 404 page

$ Fontsize = "2 ";

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

$ Bgcolor = "# ffffff ";

# Text color youd like to use 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 = dont use the email capabilities at all
#1 = send email only if the errors 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 shocould be
# Notified of 404 errors. Dont escape the @ symbol. This will also
# Be used as the "from" address on any emails the script generates.
# You can leave this unassigned if youre not using email features.

$ Emailaddress = "you@your.domain.com ";

 

######################################## ########################
# Dont edit below this line unless you know what youre doing #
######################################## ########################
# If you want to edit the script, Ive commented profusely :)#
######################################## ########################

 

# The print_details function is what prints the 404 error
# The visitor. As far as I know, PHP3 doesnt in1_ate Perls
# 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

# Print the 404 error in web format
Echo"404 Not Found";
Echo"";
Echo"404 Not Found";
Echo"

";
Echo "Were sorry. The page you requested, $ docroot $ REQUEST_URI, doesnt exist ";
Echo "on this server.

";

# If an email report is being generated, let the visitor know:
If ($ reportlevel! = 0)
{
Echo"

";
Echo "The details of this error have automatically been mailed to the webmaster .";
}

# Close up the HTML tags
# Echo"";

Return;
}


# The send_email function sends the details of the 404 error to
# 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.
# Using date () likely wowould have been better, but I already had this code
# Elsewhere, and Im lazy.
$ Today = getdate ();
$ Month = $ today [mon];
$ Mday = $ today [mday];
$ Year = $ today [year];
$ Hours = $ today [hours];
$ Minutes = $ today [minutes];
$ Errortime = "$ month/$ mday/$ year at $ hours: $ minutes ";

# Create the body of the email message
$ Message. = "404 Error ReportA 404 error was encountered by $ REMOTE_ADDR ";
$ Message. = "on $ errortime .";
$ Message. = "The URI which generated the error is: $ docroot $ REQUEST_URI ";
$ Message. = "The referring page was: $ HTTP_REFERER ";

# 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 users browser
Print_details ();

# See whether or not we shoshould 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;

?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.