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

PHP
基于HTTP长连接的"服务器推"技术的php 简易聊天室
PHP 程序员应该使用的10个组件
phpmailer 中文使用说明(简易版)
php 调用远程url的六种方法小结
PHP+XML 制作简单的留言本 图文教程
PHP+MySQL 制作简单的留言本
初学CAKEPHP 基础教程
网页游戏开发入门教程二(游戏模式+系统)
网页游戏开发入门教程三(简单程序应用)
PHP 向右侧拉菜单实现代码,测试使用中
PHP 压缩文件夹的类代码
PHP CKEditor 上传图片实现代码
php 将excel导入mysql
php 向访客和爬虫显示不同的内容
php实现网站插件机制的方法
PHP 远程关机实现代码
超级简单的php+mysql留言本源码
PHP 面向对象实现代码
php 分库分表hash算法
计算一段日期内的周末天数的php代码(星期六,星期日总和)

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


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

?>