This topic is locked

Login Logs

5/2/2007 10:53:11
ASPRunnerPro General questions
J
JOHNF777 author

Has anyone created a log table for when users login?
What's the best approach for tracking users logging in?
I was thinking of creating a table in MSSQL with

LogDate - Time/Date of login,

USERID - username

MACHIP - IP address of the machine.
Can someone help me how to to the add record to another table?
Which part of the General Event should I add the code.
Thanks,

John

F
funklet 5/2/2007

John,
I use the following code for tracking users:
[codebox]Sub AfterSuccessfulLogin()

'** Insert a record into another table ****
Session("UserID") = UCase(Session("UserID"))
RAIP = Request.ServerVariables("REMOTE_ADDR")

FWIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
strSQLIns = "insert into IPLog (CUSTCODE, REMOTE_ADDR, HTTP_X_FORWARDED_FOR) values ('" & Session("UserID") & "', '" & RAIP & "', '" & FWIP & "')"

dbConnection.Execute strSQLIns
End Sub[/codebox]
IPLog is the name of the table being used.

RAIP is the IP of the computer / NAT router that they are using.

If they are using a Proxy server then this will be IP of the Proxy server.

FWIP is the IP of the computer, if available if they are using Proxy server.
The code is in:
Events Step 11

Global Events

Login Page

After successful login.
The only drawback with this is if the page times out through inactivity then when they log back in this is registered as a new login, even if perhaps the user simply went to make a cup of tea or answered the phone.
To change the timeout, if you are using locally hosted IIS then you can change it on the IIS Manager / Website Properties / Home Directory Tab / Configuration / Options Tab / Session timeout.
This variable is how long the Session of the user will take to timeout through inactivity if they do not log off manually (most users simply close the browser window)

Unless your site is very busy setting a fairly long session timeout e.g. 1 hour should be more than enough to prevent double entries in the log.

If your site is busy then it is not advised to make this value any more than enough to reasonably prevent double entries.

Each session takes up a small amount of RAM so the more sessions that are active, the more RAM used.
To convert what you have asked to reality the code would be something like:
[codebox]Sub AfterSuccessfulLogin()

'** Insert a record into another table ****
LogDate = now()

USERID = UCase(Session("UserID"))

MACHIP = Request.ServerVariables("REMOTE_ADDR")
strSQLIns = "insert into IPLog (LogDate, USERID, MACHIP ) values ('" & LogDate & "', '" & USERID & "', '" & MACHIP & "')"

dbConnection.Execute strSQLIns
End Sub[/codebox]
Where IPLog is the name of the table in MSSQL with the Fields of the appropriate data type LogDate - DateTime / USERID - varchar(100) / MACHIP - varchar(15)
I hope this is of help.
Kind Regards.