asp如何检测自己网站的错误--asp编程技术
广告投放★自助友情CMS落伍广告联盟晒乐广告联盟脉动广告联盟品味广告联盟
广告位可自定样式联系QQ:38355018个文字广告月20元广告联系QQ:3835501广告位可自定样式
8个文字广告月20元黄金广告位每月20元广告位可自定样式联系QQ:3835501广告位可自定样式
左旋肉碱、全国包邮
买二送一、无效退款

文章浏览→编程相关Asp编程→asp如何检测自己网站的错误--asp编程技术

asp如何检测自己网站的错误--asp编程技术
asp如何检测自己网站的错误--asp编程技术
asp编程技术

下面是根据wrox的professional asp3.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><fontcolor="#999999"> 您正在搜索的网页可能已经删除、更名或暂时不可用。</font></b>
<%
'On Error Resume Next
strTarget = Request.ServerVariables("QUERY_STRING")
strReferer = Request.ServerVariables("HTTP_REFERER")

intSemiColon = InStr(strTarget, ";") 'getthe 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 anotherpage
Response.Write "<font style=""COLOR:000000; FONT: 9pt/11pt宋体"">下列网页中发现了链接错误: " & strReferer _
& " 。</font><BR>"
Set oConn = Server.CreateObject("ADODB.Connection") 'to store thedetails
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 theirbrowser
End If
%>
<!--这里的连接是你的域名-->
<p> <a href="http://你的域名"><fontcolor="#993399">打开主页</font></a>,寻找指向所需信息的链接。
</p>
<p> 单击<a href="javascript:history.back(1)"><fontcolor="#0033CC">后退</font></a>按钮尝试其他链接。</p>
<hr>
<p> <a href="http://你的域名"><fontcolor="#993399">你的网站</font></a>制作,意见和建议请联系<ahref="你的邮件"><fontcolor="#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 thenstrtarget=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><fontcolor="#999999"> 您浏览的页面出现了服务器内部错误,暂时无法打开,这个问题已经被系统记录,我们将立即解决该问题。</font></b>

<p> 单击<ahref="javascript:history.back(1)"><fontcolor="#0033CC">后退</font></a>按钮尝试其他链接。</p>
<hr>
<p> <a href="http://你的域名"><fontcolor="#993399">你的网站</font></a>制作,意见和建议请联系<ahref="mailto:你的邮件"><fontcolor="#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>
<SPANCLASS=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. Oncethe error has been fixed,<BR>
tick that entry and click the <B>Delete SelectedErrors</B> button.<P>
<FORM ACTION="<% =Request.ServerVariables("SCRIPT_NAME")%>"METHOD="POST">
<%
strSQL="SELECT DISTINCT dDateTime, ErrorType, TargetURL, ErrorInfoFROM 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 beaccessed.</B></BODY></HTML>"
Response.End
End If

'-- loop through the error records
intWinNumber = 0 'to create different target window for eachlink
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 brokenlink.<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 SelectedErrors">
<INPUT TYPE="SUBMIT" NAME="DeleteAll" VALUE="Delete AllErrors">
</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中查看你的错误的详细资料了,呵呵,这样我们就可以一眼看到我们常犯的低级错误,而一举把他们消灭,你的网站就会显得“干净多了”。

所属分类:编程相关Asp编程    作者:新浪博客    时间:2010-11-5 19:49:00

文章导航