捕捉並保存ASP運行錯誤的函數程式碼,需要取得asp程式碼運行錯誤的朋友可以參考下過程名稱:catch(str)
使用方法:
複製代碼代碼如下:
on error resume next
'你的程式碼,如資料庫連接
call catch(顯示給使用者的提示訊息)
功能:清除IIS的錯誤提示訊息,自訂錯誤提示傳回給用戶,並將出錯訊息儲存到txt檔案(當然你也可以稍做修改轉向自訂頁面等)
代碼:
複製代碼代碼如下:
<%
option explicit
'例一---------------------------
'必須和on error resume next一起使用,但在網頁沒有正式發布之前最好將其註釋掉,以免在調試時看不到出錯詳細信息
on error resume next
'i沒有定義,會出錯,使用catch清除錯誤並儲存到記事本
i
call catch(頁面無法存取)
'-------------------------------
'例二--------------------------
function conn()
'必須和on error resume next一起使用
on error resume next
'...........你的連結資料庫程式碼
call catch(資料庫開啟錯誤)
end function
'-------------------------------
sub catch(str)
if err.number <> 0 then
dim tmp,path
'錯誤日誌絕對路徑,如/error_log.txt
path = /table/error_log.txt
tmp = tmp & 出錯頁面: & geturl & vbcrlf
tmp = tmp & 錯誤時間: & now() & vbcrlf
tmp = tmp & 來訪IP: & ip & vbcrlf
tmp = tmp & 提示訊息: & str & vbcrlf
tmp = tmp & 錯誤代號: & err.number & vbcrlf
tmp = tmp & 錯誤訊息: & err.description & vbcrlf
tmp = tmp & 應用程式: & err.source & vbcrlf & vbcrlf & vbcrlf
tmp = tmp & file_read(path)
call file_save(tmp,path,1)
err.clear()
die(str)
end if
end sub
'以下為catch所用到的函數--------------------
sub echo(str)
response.write(str)
end sub
sub die(str)
echo(str) : response.end()
end sub
function ip()
ip = request.servervariables(remote_addr)
end function
'取得目前URL
function geturl()
dim tmp
if lcase(request.servervariables(https)) = off then
tmp = http://
else
tmp = https://
end if
tmp = tmp & request.servervariables(server_name)
if request.servervariables(server_port) <> 80 then
tmp = tmp & : & request.servervariables(server_port)
end if
tmp = tmp & request.servervariables(url)
if trim(request.querystring) <> then
tmp = tmp & ? & trim(request.queryString)
end if
geturl = tmp
end function
'函數:讀取檔案內容到字串
function file_read(path)
dim tmp : tmp = false
if not file_exists(path) then file_read = tmp : exit function
dim stream : set stream = server.CreateObject(ADODB.Stream)
with stream
.type = 2 '文字類型
.mode = 3 '讀寫模式
.charset = gb2312
.open
.loadfromfile(server.MapPath(path))
tmp = .readtext()
end with
stream.close : set stream = nothing
file_read = tmp
end function
'函數:保存字串到文件
function file_save(str,path,model)
if model<>0 and model<>1 then model=1
if model=0 and file_exists(path) then file_save=true : exit function
dim stream : set stream = server.CreateObject(ADODB.Stream)
with stream
.type = 2 '文字類型
.charset = gb2312
.open
.writetext str
.savetofile(server.MapPath(path)),model+1
end with
stream.close : set stream = nothing
file_save = file_exists(path)
end function
'函數:檢測檔案/資料夾是否存在
function file_exists(path)
dim tmp : tmp = false
dim fso : set fso = server.CreateObject(Scripting.FilesyStemObject)
if fso.fileexists(server.MapPath(path)) then tmp = true
if fso.folderexists(server.MapPath(path)) then tmp = true
set fso = nothing
file_exists = tmp
end function
%>