欢迎光临
我们一直在努力

如何检测自己网站的错误(让管理员可以睡觉)

建站超值云服务器,限时71元/月

下面是根据wrox的professional asp 3.0建立的网站错误检查机制,使用了之后,你可以找到自己很多的错误,节省大量的代码测试时间,为什么不用呢!
首先建立表:

create table [dbo].[tsiteerrors] (
    [kerrkey] [int] identity (1, 1) not null ,
    [ddatetime] [datetime] not null ,
    [errortype] [varchar] (255) not null ,
    [targeturl] [varchar] (255) null ,
    [errorinfo] [varchar] (255) null
) on [primary]
go
一、检查记录网站的错误链接:
在网站的目录中建立/siteerror/naverror.asp
________________________________________________

<%@language="vbscript"%>
<%response.status = "404 not found" %>
<!–这里是数据库连接字符串的位置–>
<!–#include file="../include/connect.asp"–>

<html>
<head>
<title>无法找到网页</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="robots" content="noindex">
<style type="text/css">
<!–
td {  font-size: 9pt}
a{color:#000000;text-decoration:none;}
a:link {color: #000000; text-decoration: none}
a:visited {color: #000000; text-decoration: none}
a:active {color: #000000; text-decoration: none}
a:hover {color: #0c78d1; text-decoration: underline}
–>
</style>
</head>
<body bgcolor="#ffffff" text="#000000" topmargin="0">
<table width="100%" border="1" cellspacing="0" cellpadding="0" height="95%" bordercolor="#000000" align="center">
  <tr>
    <td bgcolor="#ffffff">
      <h2><b><font color="#993399">  无法找到网页</font></b></h2>
      <hr>
      <b><font color="#999999"> 您正在搜索的网页可能已经删除、更名或暂时不可用。</font></b>
<%
on error resume next
strtarget = request.servervariables("query_string")
strreferer = request.servervariables("http_referer")

intsemicolon = instr(strtarget, ";") get the original target
if (intsemicolon > 0) and (intsemicolon < len(strtarget)) then
  strtarget = mid(strtarget, intsemicolon + 1)
  if len(strtarget) > 254 then strtarget = left(strtarget, 254)
end if

if len(strreferer) > 255 then strreferer = left(strreferer, 255)

strinform = "<font style=""color:000000; font: 9pt/11pt 宋体"">请通知包含该错误链接的网站管理员。</font>"
strtyping = "<font style=""color:000000; font: 9pt/11pt 宋体"">  如果您在“地址”栏中键入了网页地址,请检查其拼写是否正确。或者:</font>"
strrecord = "<font style=""color:000000; font: 9pt/11pt 宋体"">这个错误已经被记录,将尽快得到处理。</a>"

response.write "<p>  无法找到的网页地址为:  " & strtarget & "<p>"
if len(strreferer) > 0 then    came from a link on another page
  response.write "<font style=""color:000000; font: 9pt/11pt 宋体"">下列网页中发现了链接错误: " & strreferer _
                  & " 。</font><br>"
  set oconn = server.createobject("adodb.connection") to store the details
  oconn.open dsn
  strsql="exec sp_errorrecorder broken link," & strtarget & ","&strreferer&""
  oconn.execute strsql
rem 注意这里
  if err.number = 0 and instr(strreferer, "你的域名 例如163.com") > 0 then
     response.write strrecord  came from a page on our site
  else
     response.write strinform  came from a page on another site
  end if
else
  response.write strtyping  they just typed it wrong into their browser
end if
%>
<!–这里的连接是你的域名–>
      <p> <a href="http://你的域名"><font color="#993399">打开主页</font></a>,寻找指向所需信息的链接。
      </p>
      <p> 单击<a href="javascript:history.back(1)"><font color="#0033cc">后退</font></a>按钮尝试其他链接。</p>
      <hr>
      <p> <a href="http://你的域名"><font color="#993399">你的网站</font></a>制作,意见和建议请联系<a href="你的邮件"><font color="#0033cc">你的邮件</font></a>
      </p>
      <p> </p>
      <p> </p>
      </td>
  </tr>
  <tr>
    <td height="5" bgcolor="#000000"></td>
  </tr>
</table>
</body>
</html>

二、检查记录网站的代码错误:
在网站的目录中建立/siteerror/asperror.asp

<%@language="vbscript"%>
<% response.status="500 internal error"%>
<!–#include file=../include/connect.asp–>
<html>
<head>
<meta name="robots" content="noindex">
<title>服务器内部错误</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!–
td {  font-size: 9pt}
a{color:#000000;text-decoration:none;}
a:link {color: #000000; text-decoration: none}
a:visited {color: #000000; text-decoration: none}
a:active {color: #000000; text-decoration: none}
a:hover {color: #0c78d1; text-decoration: underline}
–>
</style>
</head>

<body bgcolor="#ffffff" text="#000000" topmargin="0">
<%
response.flush
on error resume next
set objasperror=server.getlasterror()
strerrnumber=cstr(objasperror.number)
straspcode=objasperror.aspcode
strerrdescription=objasperror.description
straspdescription=objasperror.aspdescription
strcategory=objasperror.category
strfilename=objasperror.file
strlinenum=objasperror.line
strcolnum=objasperror.column
if isnumeric(strcolnum) then
   lngcolnum=clng(strcolnum)
else
   lngcolnum=0
end if
strsourcecode=objasperror.source

set objasperror=nothing

if len(strfilename) then
   strtarget=strfilename
else
   strtarget="**file name not available **"
end if

if len(straspcode) then
   strinfo="" & straspcode & ""
else
   strinfo="error"
end if

if len(strcategory) then
   strinfo=strinfo & " in " & strcategory & ".<br>"
end if

strinfo=strinfo & "code: " & strerrnumber & "(0x" & hex(strerrnumber) & ") " & strerrdescription & ".<br>"
if len(straspdescription) then
   strinfo=strinfo&"asp reports: " & straspdescription & ".<br>"
end if

if strlinenum>"0" then
   strinfo=strinfo & "line " & strlinenum
   if lngcolnum>0 then
      strinfo=strinfo & ". column " & lngcolnum
      if len(strsourcecode) then
         strinfo=strinfo & " " & strsourcecode
      end if
   end if
end if

if len(strtarget) >255 then strtarget=left(strtarget,255)
if len(strinfo)>255 then strinfo = left(strinfo,255)
strinfo=replace(strinfo,"","")
set oconn=server.createobject("adodb.connection")
oconn.open dsn
strsql="insert into tsiteerrors(errortype,targeturl,errorinfo) values (asp error," & strtarget & "," & strinfo & ")"
strsql="exec sp_errorrecorder asp error," & strtarget & ","&strinfo&""
oconn.execute strsql
oconn.close
set oconn=nothing
%>
<table width="100%" border="1" cellspacing="0" cellpadding="0" height="95%" bordercolor="#000000" align="center">
  <tr>
    <td bgcolor="#ffffff">
      <h2><b><font color="#993399">  服务器内部错误</font></b></h2>
      <hr>
      <b><font color="#999999"> 您浏览的页面出现了服务器内部错误,暂时无法打开,这个问题已经被系统记录,我们将立即解决该问题。</font></b>
     
      <p> 单击<a href="javascript:history.back(1)"><font color="#0033cc">后退</font></a>按钮尝试其他链接。</p>
      <hr>
      <p> <a href="http://你的域名"><font color="#993399">你的网站</font></a>制作,意见和建议请联系<a href="mailto:你的邮件"><font color="#0033cc">你的邮件</font></a>
      </p>
      <p> </p>
      <p> </p>
      </td>
  </tr>
  <tr>
    <td height="5" bgcolor="#000000"></td>
  </tr>
</table>
</body>
</html>

三、建立检查错误的页面:
/siteerror/manage_error.asp

<%@language="vbscript"%>
<!– #include virtual="/include/connect.asp" –>
<%
server.scripttimeout = 900
response.expires = 0
%>

<html>
<head>
<meta name="robots" content="noindex,nofollow">
<title>web-developer site administration</title>
<style type="text/css">
body {font-family:"arial"; font-size:12; font-weight:normal}
.intro {font-family:"arial"; font-size:14; font-weight:bold}
td {font-family:"arial"; font-size:12; font-weight:normal}
th {font-family:"arial"; font-size:12; font-weight:bold; text-align:left}
h1 {font-family:"arial"; font-size:24; color:darkgray}
</style>
</head>
<body bgcolor="#ffffff">
<h1>网站错误检查</h1>
<hr>
<span class=intro>管理网站的错误</span><p>

<%
on error resume next

***********************************************
— open database connection
***********************************************
set oconn = server.createobject("adodb.connection")
oconn.open dsn

************************************************
— delete all items from table
************************************************
if len(request.form("deleteall")) then
    strsql = "delete from tsiteerrors"
    oconn.execute(strsql)
    if err.number = 0 then response.write "已经删除所有错误<br>"
end if

if len(request.form("deletechecked")) then
************************************************
— delete ticked items from table
************************************************
for each chkbox in request.form
  if left(chkbox, 1) = "x" then
    strtargeturl = mid(chkbox,2)
    strsql = "delete from tsiteerrors where targeturl=" & strtargeturl & ""
    oconn.execute(strsql)
    if err.number = 0 then response.write "deleted errors for target: <b>" & strtargeturl & "</b><br>"
  end if
next
************************************************
end if

************************************************
— display list of errors
************************************************
%>
<p>click on a link to view the source or target page. once the error has been fixed,<br>
tick that entry and click the <b>delete selected errors</b> button.<p>
<form action="<% = request.servervariables("script_name")%>" method="post">
<%
strsql="select distinct ddatetime, errortype, targeturl, errorinfo from tsiteerrors order by ddatetime desc"
set ors = oconn.execute(strsql)
if (ors.eof) or (err.number > 0) then
  response.write "<b>sorry, the database cannot be accessed.</b></body></html>"
  response.end
end if

— loop through the error records
intwinnumber = 0   to create different target window for each link
do while not ors.eof
   if ors("errortype") = "broken link" then
      — display broken link details %>
      the page <a href="<% = ors("errorinfo") %>" target="new_err_win<% = intwinnumber %>"><% = ors("errorinfo") %></a> contains a broken link.<br>
      target is <a href="<% = ors("targeturl") %>" target="new_err_win<% = intwinnumber + 1 %>"><% = ors("targeturl") %></a><br>
      date/time: <% = ors("ddatetime") %>    
   <%
   else
      — display script error details %>
      the page <a href="<% = ors("targeturl") %>" target="new_err_win<% = intwinnumber %>"><% = ors("targeturl") %></a> produced an asp error .<br>
      <% = server.htmlencode(ors("errorinfo")) %><br>
      date/time: <% = ors("ddatetime") %>    
   <%
   end if
   %>
   <input type="checkbox" name="x<% = server.htmlencode(ors("targeturl")) %>">delete?<p>
   <%
   intwinnumber = intwinnumber + 2
   ors.movenext
loop
set ors = nothing
set oconn = nothing
%>
<hr>
<input type="submit" name="deletechecked" value="delete selected errors">    
<input type="submit" name="deleteall" value="delete all errors">
</form>
</body>
</html>

四、在internet服务管理器中设置自定义错误:

在internet服务管理器中选择属性中的[自定义错误信息],编辑404[错误连接]的属性,[消息类型]改为url,url改成/siteerror/naverror.asp,编辑500[内部错误]的属性,[消息类型]改为url,url改为/siteerror/asperror.asp,然后确定。

五、现在你就可以在http://127.0.0.1/siteerror/manage_error.asp中查看你的错误的详细资料了,呵呵,这样我们就可以一眼看到我们常犯的低级错误,而一举把他们消灭,你的网站就会显得“干净多了”。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何检测自己网站的错误(让管理员可以睡觉)
分享到: 更多 (0)