|
Creating A Log File
:
Are you interested to know who came to your site? at what time? and what was the
referring URL. Here is an example how to make a Log file for an ASP page to keep
track of users on your site. With this Log file you will be able to check who
visited your site, At what time, What was the reffering URL and what browser
that user was using.
This Example use a text file to write user information. If you want to learn
more about how to write to a text file within an ASP page Click here.
Create a file LogFile.asp with the following code and add in the header of every
page useing Server Side #Include.
<!--#Include File="LogFile.asp"-->
When some one will come to your site on any page which has LogFile.asp included
that user's information will be written to LogFile.txt. If the refering url is
from same site, LogFile.asp will not write that information.
File: LogFile.asp <%
Dim blnValidEntry ' Log variable
' First set that this log is valid
blnValidEntry = True
' If Session Variable "LogIn" is not empty
' that mean this person has already been logged
' then set blnValidEntry to False
If not IsEmpty(Session("LogIn")) then blnValidEntry = False
' Here you can add different restriction
' If the refering url is from same site
' then there is no need to write to log file
If Left(Request.ServerVariables("HTTP_REFERER"), 17)="http://devasp.com" Then
blnValidEntry = False
End If
If Left(Request.ServerVariables("HTTP_REFERER"), 21)="http://www.devasp.com"
Then
blnValidEntry = False
End If
' Now if blnValidEntry is True then enter to log file
If blnValidEntry 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 the required information in a string Called 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 ""
' Now Create a session varialbe to check next time for ValidEntry
Session("LogIn") = "yes"
Set TS = Nothing
Set FSO = Nothing
End If
%>
|