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

PHP
如何用PHP脚本和PEAR类创建ZIP档案文件
在线管理PHP网站文件
利用PHP代码实现网页自动判断转向
PHP程序中的特效应用 实用代码珍藏
如何使用PHP和PEAR进行不同时区的转换
如何用php生成WAP页面
php:树形结构的算法 4
php:树形结构的算法 3
php:树形结构的算法 2
php:树形结构的算法1
apache 环境下 php 的配置
php编写大型网站问题集
php中文乱码问题及解决方法
草根的进化 PHP语言发展简史
测试 Apache Web 和 PHP 应用程序服务器
用php实现简单的滑动菜单
php分页类
基于PHP和AJAX创建RSS聚合器
PHP下一代的五个framework介绍
搜索引擎技术核心揭密(PHP版)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 42 ::
收藏到网摘: 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;

?>