当前位置: 首页 > 图文教程 > 网络编程 > PHP > 用php制作友好的404错误页面

PHP
PHP中for循环语句的几种“变态”用法
用PHP与XML联手进行网站开发
PHP程序漏洞产生的原因和防范方法
利用PHP编程防范XSS跨站脚本攻击
使用PHP往Windows系统中添加用户
PHP Shell的编写(改进版)
PHP开发中接收复选框信息的方法
PHP程序加速探索之服务器负载测试
PHP实现首页自动选择语言转跳
十天学会php之第一天
十天学会php之第二天
十天学会php之第三天
十天学会php之第四天
十天学会php之第五天
十天学会php之第六天
十天学会php之第七天
十天学会php之第八天
十天学会php之第九天
十天学会php之第十天
Web开发源代码:PHP生成静态页面的类

PHP 中的 用php制作友好的404错误页面


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

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

以下为引用的内容:

<?php
# 设置 $domain 为你的域名 (注意没有www)
$domain = "oncoding.net";
#
设置URL,注意没有后划线 /
$docroot = "http://oncoding.net";
#
设置错误信息的字体
$fontface = "Verdana";
#
设置404页面的字体大小
$fontsize = "2";
#
设置404页面的背景颜色,缺省是白色
$bgcolor = "#ffffff";
#
设置文字颜色,缺省是黑色
$textcolor = "#000000";

#
使用 $reportlevel 变量来控制是否发信给网管
# 0 =
根本不用发信,嘿,NB的我们怎么会出错呢
# 1 =
只有在页面含有你的DOMAIN NAME时才发信
# 2 =
即使是与我连接出现的断连也发信,有可能是友情站点
$reportlevel = 2;  //
这种最保险了

$emailaddress = "[email protected]"; //
设置收错误信息的邮箱


function print_details()
  {
   # Request access to the global variables we need
   global $fontface, $fontsize, $docroot, $REQUEST_URI, $reportlevel;
   global $bgcolor, $textcolor;

   # Print the 404 error in web format
   echo "<html><head><title>404
没有找到页面</title></head>";
   echo "<body bgcolor="$bgcolor" text="$textcolor">";
   echo "<b><h1>404
对不起,我没有找到您要求的页面</h1></b>";
   echo "<p><font face="$fontface" size="$fontsize">";
   echo "
oncoding编码营提醒您,您要求的页面 $docroot$REQUEST_URI, doesn't exist";
   echo " on this server.</font></p>";

   if ($reportlevel != 0)
     {
      echo "<p><font face="$fontface" size="$fontsize">";
      echo "
错误信息已经发送到oncoding编码营管理员信箱.";    
     }

   return;
  }


# EMAIL
处理函数  

function send_email()
  {
   # Request access to the global variables we need
   global $REQUEST_URI, $HTTP_REFERER, $emailaddress, $REMOTE_ADDR, $docroot;

   #
定制发送的消息,如时间地点等.
   $today = getdate();  
   $month = $today[mon];  
   $mday = $today[mday];  
   $year = $today[year];  
   $hours = $today[hours];
   $minutes = $today[minutes];
   $errortime = "$month/$mday/$year at $hours:$minutes";  

   # Create the body of the email message
   $message .= "404 Error ReportnnA 404 error was encountered by $REMOTE_ADDR";
   $message .= " on $errortime.nn";
   $message .= "The URI which generated the error is: n$docroot$REQUEST_URInn";
   $message .= "The referring page was:n$HTTP_REFERERnn";

   # Send the mail message. This assumes mail() will work on your system!
   mail("$emailaddress", "404 Error Report", $message, "From: $emailaddress");  //
发送信息 
    
   return;
  }


#
下面这些是根据变量$reportlevel的设置来发信与否。
print_details();

# See whether or not we should send an email report. If so, do it.
if ($reportlevel != 0)                   
  if ($reportlevel == 1) {               
    if (eregi($domain,$HTTP_REFERER))    
      send_email(); }
  else
     send_email();                        

# All done!
exit;

?>