当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP:404错误陷阱并email给管理员的程序
<?
#404.php,8/10/2000.
#Traps404errorsandmailsanoticetothewebmaster.
#RequiresPHP3.0ornewer,andmailcapabilityonyoursystem.
#
#Copyright2000[email protected]undertheGNUPublicLicense.
#Disclaimer:Iwrotethisscriptforme,anditworksforme.
#Ifitdoesn'tworkforyou,ormakesyourserverexplode,
#that'slife.Pleaseemailwithquestionsorbugreports.
#Setthesevariablestoconfigurethescript:
#Set$domaintoyourdomainname(nowww)
$domain="your.domain.com";
#Set$docroottotheURLofthedirectorywhichcontainsyour
#.htaccessfile.Don'tincludetrailingslash.
$docroot="http://your.domain.com";
#Fontfaceyou'dliketouseonthe404page
$fontface="Verdana";
#Fontsizeyou'dliketouseonthe404page
$fontsize="2";
#Backgroundcolorofthe404page(defaultiswhite)
$bgcolor="#ffffff";
#Textcoloryou'dliketouseonthe404page(defaultisblack)
$textcolor="#000000";
#Thisscriptiscapableofmailingthedetailsofeach404error
#tothewebmaster.Usethe$reportlevelvariabletocontrolwhen
#youreceivethesereports.
#
#0=don'tusetheemailcapabilitiesatall
#1=sendemailonlyiftheerror'sreferercontainsyourdomainname
#(i.e.the404wasgeneratedbyabrokenlinkonyoursite)
#2=sendemailanytimea404errorisgenerated(usefulfortracking
#brokenlinksatothersiteswhichlinktoyou)
$reportlevel=2;
#Set$emailaddresstotheemailaddressofwhoevershouldbe
#notifiedof404errors.Don'[email protected]
#beusedasthe"from"addressonanyemailsthescriptgenerates.
#Youcanleavethisunassignedifyou'renotusingemailfeatures.
$emailaddress="[email protected]";
################################################################
#DON'TEDITBELOWTHISLINEUNLESSYOUKNOWWHATYOU'REDOING#
################################################################
#Ifyouwanttoeditthescript,I'vecommentedprofusely:)#
################################################################
#Theprint_detailsfunctioniswhatprintsthe404errorto
#thevisitor.AsfarasIknow,PHP3doesn'tincorporatePerl's
#print<<"EOT"ability.PHP4doesallowthiscapability
#butthescriptwaswrittenforPHP3.So,youhavetouse
#alotofechostatementsifyouwanttoretainPHP3compat.
functionprint_details()
{
#Requestaccesstotheglobalvariablesweneed
global$fontface,$fontsize,$docroot,$REQUEST_URI,$reportlevel;
global$bgcolor,$textcolor
#Printthe404errorinwebformat
echo"<html><head><title>404NotFound</title></head>";
echo"<bodybgcolor=\"$bgcolor\"text=\"$textcolor\">";
echo"<b><h1>404NotFound</h1></b>";
echo"<p><fontface=\"$fontface\"size=\"$fontsize\">";
echo"We'resorry.Thepageyourequested,$docroot$REQUEST_URI,doesn'texist";
echo"onthisserver.</font></p>";
#Ifanemailreportisbeinggenerated,letthevisitorknow:
if($reportlevel!=0)
{
echo"<p><fontface=\"$fontface\"size=\"$fontsize\">";
echo"Thedetailsofthiserrorhaveautomaticallybeenmailedtothewebmaster.";
}
#CloseuptheHTMLtags
#echo"</body></html>";
return;
}
#Thesend_emailfunctionsendsthedetailsofthe404errortothe
#webmaster.
functionsend_email()
{
#Requestaccesstotheglobalvariablesweneed
global$REQUEST_URI,$HTTP_REFERER,$emailaddress,$REMOTE_ADDR,$docroot;
#Buildthe$errortimevariabletocontainthedate/timeoftheerror.
#Usingdate()likelywouldhavebeenbetter,butIalreadyhadthiscode
#elsewhere,andI'mlazy.
$today=getdate();
$month=$today[mon];
$mday=$today[mday];
$year=$today[year];
$hours=$today[hours];
$minutes=$today[minutes];
$errortime="$month/$mday/$yearat$hours:$minutes";
#Createthebodyoftheemailmessage
$message.="404ErrorReport\n\nA404errorwasencounteredby$REMOTE_ADDR";
$message.="on$errortime.\n\n";
$message.="TheURIwhichgeneratedtheerroris:\n$docroot$REQUEST_URI\n\n";
$message.="Thereferringpagewas:\n$HTTP_REFERER\n\n";
#Sendthemailmessage.Thisassumesmail()willworkonyoursystem!
mail("$emailaddress","404ErrorReport",$message,"From:$emailaddress");
return;
}
#Donewithfunctiondeclarations.Mainfunctionbeginshere.
#Senda404errortotheuser'sbrowser
print_details();
#Seewhetherornotweshouldsendanemailreport.Ifso,doit.
if($reportlevel!=0)
if($reportlevel==1){
if(eregi($domain,$HTTP_REFERER))
send_email();}
else
send_email();
#Alldone!
exit;
?>
评论 (0) All