如何用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)="http://devasp.com" Then   
15ValidEntry = False   
16End if   
17If Left(Request.ServerVariables("HTTP_REFERER"), 21)="http://www.devasp.com" Then   
18ValidEntry = False   
19End If   
20  
21' Now if ValidEntry is True then enter to log file   
22If ValidEntry Then   
23Const ForAppending = 8   
24Const Create = true   
25Dim FSO   
26DIM TS   
27DIM MyFileName   
28Dim strLog   
29  
30MyFileName = Server.MapPath("MyLogFile.txt")   
31Set FSO = Server.CreateObject("Scripting.FileSystemObject")   
32Set TS = FSO.OpenTextFile(MyFileName, ForAppending, Create)   
33  
34' Store all required values in strLog   
35strLog = "

<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