如果访问您站点的用户由于各种原因暂时出现无法访问页面的错误,如何给他一个友好的答复,并且你也知道发生了这个错误,看看下面这段程序,是用来定制404错误页面和发通知给网管。
折叠 展开 PHP Code 复制内容到剪贴板
设置 $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 "
1<html><head><title>404 没有找到页面</title></head>" ;
2 32. echo "<body bgcolor=" $bgcolor " text=" $textcolor ">" ;
3 33. echo "<b><h1>404 对不起,我没有找到您要求的页面</h1></b>" ;
4 34. echo "<p><font face=" $fontface " size=" $fontsize ">" ;
5 35. echo "oncoding编码营提醒您,您要求的页面 $docroot$REQUEST_URI, doesn't exist" ;
6 36. echo " on this server.</font></p>" ;
7 37. 38. if ( $reportlevel != 0)
8 39. {
9 40. echo "<p><font face=" $fontface " size=" $fontsize ">" ;
10 41. echo "错误信息已经发送到oncoding编码营管理员信箱." ;
11 42. }
12 43. 44. return ;
13 45. }
14 46. 47. 48. # EMAIL处理函数
15 49. 50. function send_email()
16 51. {
17 52. # Request access to the global variables we need
18 53. global $REQUEST_URI , $HTTP_REFERER , $emailaddress , $REMOTE_ADDR , $docroot ;
19 54. 55. # 定制发送的消息,如时间地点等.
20 56. $today = getdate ();
21 57. $month = $today [mon];
22 58. $mday = $today [mday];
23 59. $year = $today [year];
24 60. $hours = $today [hours];
25 61. $minutes = $today [minutes];
26 62. $errortime = "$month/$mday/$year at $hours:$minutes" ;
27 63. 64. # Create the body of the email message
28 65. $message .= "404 Error ReportnnA 404 error was encountered by $REMOTE_ADDR" ;
29 66. $message .= " on $errortime.nn" ;
30 67. $message .= "The URI which generated the error is: n$docroot$REQUEST_URInn" ;
31 68. $message .= "The referring page was:n$HTTP_REFERERnn" ;
32 69. 70. # Send the mail message. This assumes mail() will work on your system!
33 71. mail( "$emailaddress" , "404 Error Report" , $message , "From: $emailaddress" ); //发送信息
34 72. 73. return ;
35 74. }
36 75. 76. 77. # 下面这些是根据变量 $reportlevel 的设置来发信与否。
37 78. print_details();
38 79. 80. # See whether or not we should send an email report. If so, do it.
39 81. if ( $reportlevel != 0)
40 82. if ( $reportlevel == 1) {
41 83. if ( eregi ( $domain , $HTTP_REFERER ))
42 84. send_email(); }
43 85. else
44 86. send_email();
45 87. 88. # All done!
46 89. exit ;
47 90. 91.
48```</font></p></body></html>