当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP:404错误陷阱并email给管理员的程序

PHP
PHP 变量类型的强制转换
PHP 判断变量类型实现代码
PHP 数组教程 定义数组
php后台程序与Javascript的两种交互方式
php 文件上传系统手记
php 网页游戏开发入门教程一(webgame+design)
PHP 简单日历实现代码
dedecms 批量提取第一张图片最为缩略图的代码(文章+软件)
php 显示指定路径下的图片
php pack与unpack 摸板字符字符含义
ThinkPHP php 框架学习笔记
PHP 批量删除数据的方法分析
PHP 读取和修改大文件的某行内容的代码
php实现jQuery扩展函数
浅谈PHP 闭包特性在实际应用中的问题
PHP 文件上传源码分析(RFC1867)
php 攻击方法之谈php+mysql注射语句构造
PHP+MySQL 手工注入语句大全 推荐
php UTF8 文件的签名问题
php 远程包含文件漏洞分析

PHP:404错误陷阱并email给管理员的程序


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 63 ::
收藏到网摘: n/a

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

?>