程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> 關於ASP編程 >> 用ASP創建日志文件(附源程序)

用ASP創建日志文件(附源程序)

編輯:關於ASP編程
這個例子使用文本文件來寫入用戶的信息創建一個logfile.asp放在每一個asp的頁面的頂端<!--#Include File="LogFile.asp"-->當有人來訪問你的站點logfile.asp自動把他的信息寫入LogFile.txt,如果相關的URl一樣的話則不寫入文件
File: LogFile.asp
復制代碼 代碼如下:
<%
Dim ValidEntry ' Log variable
' First set that this log is valid
ValidEntry = True

' If Session Variable "LogIn" is not empty
' that mean this person has already been logged
' then set ValidLog to False
If not IsEmpty(Session("LogIn")) then ValidEntry = False

' Here you can add different restriction
' If the refering url is from same site
' don't write to log file
If Left(Request.ServerVariables("HTTP_REFERER"), 17)
="http://jb51.net" Then
ValidEntry = False
End if
If Left(Request.ServerVariables("HTTP_REFERER"), 21)
="http://www.jb51.net" Then
ValidEntry = False
End If

' Now if ValidEntry is True then enter to log file
If ValidEntry Then
Const ForAppending = 8
Const Create = true
Dim FSO
DIM TS
DIM MyFileName
Dim strLog

MyFileName = Server.MapPath("MyLogFile.txt")
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set TS = FSO.OpenTextFile(MyFileName, ForAppending, Create)

' Store all required values in strLog
strLog = "<br><P><B>" & now & "</B> "
strLog = strLog & Request.ServerVariables("REMOTE_ADDR") & " "
strLog = strLog & Request.ServerVariables("HTTP_REFERER") & " "
strLog = strLog & Request.ServerVariables("HTTP_USER_AGENT") & "<BR>"
' Write current information to Log Text File.
TS.write strLog
TS.Writeline ""
' Create a session varialbe to check next time for ValidEntry
Session("LogIn") = "yes"
Set TS = Nothing
Set FSO = Nothing
End If
%>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved