投递文章 投稿指南 RSS订阅 站点通告:网站升级,部分数据正在导入之中,请等待..
您的位置:首页 >> Web编程 >> php

用php定制404错误页面 并发信通知管理员

发布时间:2009-06-21 23:26:48  文章来源:oncoding.net  浏览次数:1959  
承德互联 专业的unix(freebsd)主机 无限子域 BGP多线 自主开发面版 更快 更稳 更安全

如果访问您站点的用户由于各种原因暂时出现无法访问页面的错误,如何给他一个友好的答复,并且你也知道发生了这个错误,看看下面这段程序,是用来定制404错误页面和发通知给网管。

折叠PHP Code复制内容到剪贴板
  1. <?php    
  2. # 设置 $domain 为你的域名 (注意没有www)    
  3. $domain = "oncoding.net";    
  4. # 设置URL,注意没有后划线 /    
  5. $docroot = "http://oncoding.net";    
  6. # 设置错误信息的字体    
  7. $fontface = "Verdana";    
  8. # 设置404页面的字体大小    
  9. $fontsize = "2";    
  10. # 设置404页面的背景颜色,缺省是白色    
  11. $bgcolor = "#ffffff";    
  12. # 设置文字颜色,缺省是黑色    
  13. $textcolor = "#000000";    
  14.   
  15. # 使用 $reportlevel 变量来控制是否发信给网管    
  16. # 0 = 根本不用发信,嘿,NB的我们怎么会出错呢    
  17. # 1 = 只有在页面含有你的DOMAIN NAME时才发信    
  18. # 2 = 即使是与我连接出现的断连也发信,有可能是友情站点    
  19. $reportlevel = 2;  //这种最保险了    
  20.   
  21. $emailaddress = "[email protected]"//设置收错误信息的邮箱    
  22.   
  23.   
  24. function print_details()    
  25.   {    
  26.    # Request access to the global variables we need    
  27.    global $fontface$fontsize$docroot$REQUEST_URI$reportlevel;    
  28.    global $bgcolor$textcolor;    
  29.   
  30.    # Print the 404 error in web format    
  31.    echo "<html><head><title>404 没有找到页面</title></head>";    
  32.    echo "<body bgcolor="$bgcolor" text="$textcolor">";    
  33.    echo "<b><h1>404 对不起,我没有找到您要求的页面</h1></b>";    
  34.    echo "<p><font face="$fontface" size="$fontsize">";    
  35.    echo "oncoding编码营提醒您,您要求的页面 $docroot$REQUEST_URI, doesn't exist";    
  36.    echo " on this server.</font></p>";    
  37.   
  38.    if ($reportlevel != 0)    
  39.      {    
  40.       echo "<p><font face="$fontface" size="$fontsize">";    
  41.       echo "错误信息已经发送到oncoding编码营管理员信箱.";       
  42.      }    
  43.   
  44.    return;    
  45.   }    
  46.   
  47.   
  48. # EMAIL处理函数     
  49.   
  50. function send_email()    
  51.   {    
  52.    # Request access to the global variables we need    
  53.    global $REQUEST_URI$HTTP_REFERER$emailaddress$REMOTE_ADDR$docroot;    
  54.   
  55.    # 定制发送的消息,如时间地点等.    
  56.    $today = getdate();     
  57.    $month = $today[mon];     
  58.    $mday = $today[mday];     
  59.    $year = $today[year];     
  60.    $hours = $today[hours];    
  61.    $minutes = $today[minutes];    
  62.    $errortime = "$month/$mday/$year at $hours:$minutes";     
  63.   
  64.    # Create the body of the email message    
  65.    $message .= "404 Error ReportnnA 404 error was encountered by $REMOTE_ADDR";    
  66.    $message .= " on $errortime.nn";    
  67.    $message .= "The URI which generated the error is: n$docroot$REQUEST_URInn";    
  68.    $message .= "The referring page was:n$HTTP_REFERERnn";    
  69.   
  70.    # Send the mail message. This assumes mail() will work on your system!    
  71.    mail("$emailaddress""404 Error Report"$message"From: $emailaddress");  //发送信息    
  72.        
  73.    return;    
  74.   }    
  75.   
  76.   
  77. # 下面这些是根据变量$reportlevel的设置来发信与否。    
  78. print_details();    
  79.   
  80. # See whether or not we should send an email report. If so, do it.    
  81. if ($reportlevel != 0)                      
  82.   if ($reportlevel == 1) {                  
  83.     if (eregi($domain,$HTTP_REFERER))       
  84.       send_email(); }    
  85.   else    
  86.      send_email();                           
  87.   
  88. # All done!    
  89. exit;    
  90.   
  91. ?>   

 

作者:
与好友一起分享精彩内容:

收藏到QQ书签】【返回顶部】【返回首页
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 程序支持 - 网站地图 - 意见反馈 - 返回顶部