Click here to Skip to main content
15,887,135 members
Articles / Web Development / IIS

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 481K   9K   123  
A simple script to track users login/logout times on your site and shows how to view how many users are online.
<%@ Language=VBScript %>
<%Response.Expires = 0%>
<%Response.Buffer = true%>
<!--#include file="verify_login.asp"-->
<!--#include file="header.asp"-->

<center>
<%

'********************************
' File:	  home.asp
' Author: Horatiu Cristea
' Email:  hcristea@email.ro
' Date:   08/08/2001
'********************************


	user = Session("member")  ' Get the user ID from the session variable

	set rs_log = server.CreateObject ("ADODB.Recordset")
	
	'	This recordset has the login/logout history which is in the DB 
	query = "SELECT * FROM User_logTime WHERE offline=True AND user_id=" & user & " ORDER BY id DESC"
	rs_log.Open query, Application("connString"), 3

	' Check if the user have a login/logout history on this site.
	if not rs_log.eof then 
		set rs = server.CreateObject ("ADODB.Recordset")
		'	Get the user personal info.
		query = "SELECT * FROM Members WHERE id=" & user
		rs.Open query, Application("connString"), 3, 3

		Response.Write "<p class=FieldLabel>Log raport for: " & rs("FirstName") & " " & rs("LastName") & " (" & rs("UserName") & ")</p>"
		
		rs.Close
		'	Get the number of users that r curently online on the site.
		rs.Open "SELECT DISTINCT user_id FROM User_LogTime WHERE offline=False", Application("connString"), 3
	
		online = rs.RecordCount 
		
		rs.Close 	
		set rs = nothing
		
		'	The table in whih is displayed the login/logout history for this use
		Response.Write "<table cellspacing=2 cellpadding=2 width=50% >"
		Response.Write "<tr bgcolor=#333366>"
		Response.Write "<td><font class=colltitle>SessionID:</a></td>"
		Response.Write "<td><font class=colltitle>Login Time:</a></td>"
		Response.Write "<td><font class=colltitle>Logout Time:</a></td>"
		Response.Write "</tr>" & vbcrlf
			
		i = 1
		while not rs_Log.EOF 
			if i mod 2 then 
				Response.Write "<tr class=defaultsmall>"
			else
				Response.Write "<tr class=defaultsmall bgcolor=#dedede>"
			end if
			Response.Write "<td>" & rs_Log("SID") & "</td>"
			Response.Write "<td>" & rs_Log("Login_Time") & "</td>"
			if rs_Log("Logout_Time") <> "" then 
				Response.Write "<td>" & rs_Log("Logout_Time") & "</td>"
			else
				Response.Write "<td>N/A</td>"
			end if
			Response.Write "</tr>" & vbcrlf
			i = i + 1
			rs_Log.MoveNext 
		wend
		Response.Write "</table>"
		
		'	Display the line with the number of online users
		Response.Write "<p class=defaultsmall>" & online & " user(s) online.</p>"				
	else
		Response.Write "<p class=FieldLabel>This user login for the 1st time at this site.<br> we dont have a login/logout history for this user yet.</p>"
	end if
	
	rs_Log.Close 
	set rs_Log = nothing
	
%>

<a href='logout.asp' class=errmsg>Logout Baby!</a>

</center>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Romania Romania
Cool | :cool:

Comments and Discussions