Click here to Skip to main content
15,179,627 members
Articles / Web Development / IIS
Posted 18 Aug 2001

Tagged as


123 bookmarked

Tracking users login/logout times on your site

Rate me:
Please Sign up or sign in to vote.
4.20/5 (28 votes)
18 Aug 20014 min read
A simple script to track users login/logout times on your site and shows how to view how many users are online.

Sample Image - User_LogTime1.gif


Here is a script that track user login/logout times on a website. It's a simple script that I have used on some of the sites I've made. Also with this script you can see how many users are online at your site.

For this you need first a site with a 'login to enter' (member based community site). You also need a database to keep the users and the records of their login/logout times. You also need the global.asa file so you can use the Session_OnEnd event to track the time when Session.Abandon occurs or Session.Timeout expires. That is when a user hit logout or quits your application.

The Database

The database for this demo is a MS Access 2000 database (.mdb file) and it contains 2 tables: Members table and User_LogTimes table. In the Members table I keep the a minimal login information such as: user name, Password, user first name and user last name.

In the picture below you can see the design of the Members table. This table is not relevant for this article but I talked about it so you can understand the SQL queries that will be presented further in the article.

Members Table design

Now take a look over the design of User_LogTime table. In this table we will keep the records of the user login/logout times plus from this table we can get how many users are online on your site.

In the picture below you can see the design of the table.

User_LogTime Table Design

This is short description of the table fields.

id - Its the primary key of this table to uniquely identify each record
user_id - The id of the user from the Members table which will be shown in a picture below
SID - The Session.SessionID of each session opened on the site when a user runs for the first time any page in your application.
Login_Time - The time when the user login on the site.
Logout_Time - The time when the user logout off the site.
offline - Boolean value which is used to tell how many user are online on the site.

How this script works


When a user will login on the site, the script in login.asp will be executed first. That's why I will begin to explain this code first.

With the user and password entered in the login form you will build a query to to get the user's id value from the Members table. That value you will store in a session variable.

' Get the user name and the password from the login form
UserName = Request.Form ("UserName")
Password = Request.Form ("Password")
set conn = Server.CreateObject ("ADODB.Connection")
conn.Open Application("connString")
query = "SELECT Id FROM Members WHERE UserName='" &_
         UserName & "' AND Password='" & Password &_
' Get the user id from the database
set rs = conn.Execute (query)
' set the user id value from the Members table in a session variable
Session("member") = rs("Id")

You have the user id value in the Session("member") variable. Now we have to modify all the records in the User_LogTime for this user that have the offline field set as False (that means he is marked as being online) and set that field to True. The fields changed in this way will not have a LogOut time. Normally this operation should not affect any record in the table. Records with offline set to False may exist when they were not closed properly when the user previously logged out.

Then we will insert a new record in the User_LogTime table with the id of the user, the SessionID of this user and with the time of login.

' Modify all the records from the User_LogTime corresponding to this user.
query = "UPDATE User_LogTime SET offline=True WHERE offline=False AND user_id=" &_
conn.Execute (query)
' Insert a new record in the User_LogTime table with the user_id, 
' SessionID and the login time.
query = "INSERT INTO User_LogTime (user_id, SID, Login_Time) "
query = query &_
        "VALUES (" & Session("member") & "," &_
        Session.SessionID & ",#" & now() & "#)"
conn.Execute (query)


The code above was called when the Session began and you got the login time and wrote it in the database.

When the user hits logout then Session.Abandon will be called. If the user quit your application without hitting logout then after the session.Timeout expires Session_OnEnd will be executed. In this procedure you will update the record written in login.asp and you will update the Logout_time and the offline field to True.

Sub Session_OnEnd
	set conn = Server.CreateObject ("ADODB.Connection")
	conn.Open Application("connString")

	' Update the record when the user logout and write the logout time 
	' plus it sets the user as OFFLINE.
	query = "UPDATE User_LogTime SET Logout_Time=#" & now() & "#, offline=True "
	query = query & "WHERE offline=False AND SID=" & Session.SessionID &_
                " AND user_id=" & Session("member")
	conn.Execute (query)
	set conn = Nothing	
End Sub

That's all there is for tracking login/logout times. You have these values in the User_LogTimes and you can display them however you want. You can take a look at how I've done this in the project demo (see the 1st pic above).

Online users

Having the offline field in the database makes this very easy. You can make an SQL query like this:

query = "SELECT DISTINCT user_id FROM User_LogTime WHERE offline=False"

Execute that query and the number of records in the recordset will be the number of your online users. Alternatively you can call

query = "SELECT COUNT(*) as NumOnline FROM User_LogTime WHERE offline=False"

To return a recordset with one record and one field ("NumOnline") that has the number of online users.

See the demo application for examples.


  • when you store SessionID in the database, data type of that field are set always to a number. If you want to compare the value from that field with the Session.SessionID then this will save you for some trouble.
  • On PWS on Win95/98 this should work fine but on IIS5 remember that this Session_OnEnd will be run by IWAM_machine and not by
    so set write/modify rights on the database where you will keep the login/logout times.

Happy Programming!!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Romania Romania
Cool | :cool:

Comments and Discussions

GeneralMy vote of 3 Pin
Member 123752497-Mar-16 5:30
MemberMember 123752497-Mar-16 5:30 
QuestionTracking Logon and Logoff Activity in database in c# Pin
Balu Mate24-Apr-15 0:00
MemberBalu Mate24-Apr-15 0:00 
QuestionTracking users login/logout times using vb6 and sql server 2005 database Pin
Aleu Philip17-Apr-13 4:38
MemberAleu Philip17-Apr-13 4:38 
Questionhow to log up with database sql server 2008 Pin
jai748616-Feb-11 3:47
Memberjai748616-Feb-11 3:47 
Generalhi Pin
santhosepriya3-Nov-10 2:56
Membersanthosepriya3-Nov-10 2:56 
Generalhi nice project but i required some help Pin
santosh d25-Mar-10 3:54
Membersantosh d25-Mar-10 3:54 
GeneralRe: hi nice project but i required some help Pin
Horatiu CRISTEA25-Mar-10 4:10
MemberHoratiu CRISTEA25-Mar-10 4:10 
GeneralHELP ME !!!! Pin
mariachen14-Oct-07 22:16
Membermariachen14-Oct-07 22:16 
GeneralRe: HELP ME !!!! Pin
Horatiu CRISTEA14-Oct-07 22:33
MemberHoratiu CRISTEA14-Oct-07 22:33 
GeneralRe: HELP ME !!!! Pin
mariachen14-Oct-07 23:07
Membermariachen14-Oct-07 23:07 
GeneralRe: HELP ME !!!! Pin
Horatiu CRISTEA14-Oct-07 23:11
MemberHoratiu CRISTEA14-Oct-07 23:11 
GeneralRe: HELP ME !!!! Pin
mariachen14-Oct-07 23:23
Membermariachen14-Oct-07 23:23 
GeneralRe: HELP ME !!!! Pin
Horatiu CRISTEA14-Oct-07 23:42
MemberHoratiu CRISTEA14-Oct-07 23:42 
GeneralRe: HELP ME !!!! Pin
mariachen14-Oct-07 23:50
Membermariachen14-Oct-07 23:50 
GeneralHELP ME !!!! Pin
mariachen14-Oct-07 22:15
Membermariachen14-Oct-07 22:15 
Generalset session time out Pin
rompi8-May-07 21:44
Memberrompi8-May-07 21:44 
GeneralI am using .net Pin
LovelyHelp15-Mar-06 23:36
MemberLovelyHelp15-Mar-06 23:36 
GeneralRe: I am using .net Pin
friends_pressy26-Sep-07 7:23
Memberfriends_pressy26-Sep-07 7:23 
GeneralI need some help! Pin
jure_split10-Nov-05 10:56
Memberjure_split10-Nov-05 10:56 
Generaltracking user all activities Pin
Member 204379313-Jun-05 23:48
MemberMember 204379313-Jun-05 23:48 
QuestionHow to record the Logout time? Pin
ro_B5-Jun-05 23:42
Memberro_B5-Jun-05 23:42 
AnswerRe: How to record the Logout time? Pin
Member 1393213813-Aug-18 20:48
MemberMember 1393213813-Aug-18 20:48 
Generalsession on_end event does not work Pin
dhanashree2978123-May-05 4:29
Memberdhanashree2978123-May-05 4:29 
Generalremote SQL server Pin
joeX5127-Dec-04 11:40
MemberjoeX5127-Dec-04 11:40 
Questiontoo few parameters? Pin
jessie_ncy6-Jul-04 18:22
Memberjessie_ncy6-Jul-04 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.