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

你是否有时想知道什么人访问你的站点,什么时间,IP地址等。下面我就这个问题向
大家来阐述一下。这个例子使用文本文件来写入用户的信息创建一个logfile.asp放
在每一个asp的页面的顶端

当有人来访问你
的站点logfile.asp自动把他的信息写入LogFile.txt,如果相关的URl一样的话则不
写入文件
File: LogFile.asp

 1   
 2Dim ValidEntry ' Log variable   
 3' First set that this log is valid   
 4ValidEntry = True   
 5  
 6' If Session Variable "LogIn" is not empty   
 7' that mean this person has already been logged   
 8' then set ValidLog to False   
 9If not IsEmpty(Session("LogIn")) then ValidEntry = False   
10  
11' Here you can add different restriction   
12' If the refering url is from same site   
13' don't write to log file   
14If Left(Request.ServerVariables("HTTP_REFERER"), 17)   
15="http://devasp.com" Then   
16ValidEntry = False   
17End if   
18If Left(Request.ServerVariables("HTTP_REFERER"), 21)   
19="http://www.devasp.com" Then   
20ValidEntry = False   
21End If   
22  
23' Now if ValidEntry is True then enter to log file   
24If ValidEntry Then   
25Const ForAppending = 8   
26Const Create = true   
27Dim FSO   
28DIM TS   
29DIM MyFileName   
30Dim strLog   
31  
32MyFileName = Server.MapPath("MyLogFile.txt")   
33Set FSO = Server.CreateObject("Scripting.FileSystemObject")   
34Set TS = FSO.OpenTextFile(MyFileName, ForAppending, Create)   
35  
36' Store all required values in strLog   
37strLog = "

<br/>

<p><b>" &amp; now &amp; "</b> "
strLog = strLog &amp; Request.ServerVariables("REMOTE_ADDR") &amp; " "
strLog = strLog &amp; Request.ServerVariables("HTTP_REFERER") &amp; " "
strLog = strLog &amp; Request.ServerVariables("HTTP_USER_AGENT") &amp; "<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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus