ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
VBScript Classes: Part 1 of N
Migrating to ASP.NET
Getting Scripts to Run on a Schedule

QUICK TIP:
Cache HTTP Requests
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Who Are Those Active Users?

Who Are Those Active Users?

by John Peterson

Introduction

Sometimes you write something and no one reads it. Other times you write something and not only does everyone read it, they then proceed to never stop asking you about it! (Speaking of which, I'd like to thank Christopher A. Nowell for the title of this article since it came directly from his email's subject line. Thanks!)

Well the latter seems to have been the case with my original Counting Active Users article. Many of the questions have been questions about getting the global.asa file to process which I subsequently covered in the addendum and in the Applications, Sessions, and Global.asa article. In addition to these simple configuration issues, there seemed to be a second group of questions from people who had gotten the original script to work, but then decided they wanted just a little bit more information about the users on their site.

That's where this article comes into the picture. In it I'm going to take the original code and the ideas from the first article and build on them to provide a little bit better system. That being said, it's still going to be pretty generic so it'll work for everyone, but it should illustrate the point and allow you to modify it to fit your specific needs.

The Concept

The basic concept is similar to the old system -- when a user comes to the site you gather information about them and store it somewhere for later use. Then when they leave you dispose of this information. The original script just stored one number. It got increased whenever a new user came to the site and decreased when they left. Simple, clean, and neat... but there's not really much information there and almost nothing you can derive from it, except the current number of visitors. (Which, to be fair to the code, is all that it was meant to tell you!)

However almost no one seems to be happy with that. Everyone immediately wants a list of the users so that's what this article will cover. The main thing that makes this a little more difficult is that we now need to store more information and we need to be able to associate each user with their own particular set of information. Now there are lots of ways to do this, but in the tradition of the first article I'm going to store everything in an Application variable. Only this time it's not going to be an integer, but instead a recordset.

Instead of describing the code I'm going to just show you the code inline and comment heavily. So here goes:

The Code

Global.asa

<object runat="Server" scope="Application"
id="rstActiveUsers" progid="ADODB.Recordset">
</object>
<script language="VBScript" runat="Server">
' The first thing you should notice is the top line.
' It creates an application scoped recordset object
' named rstActiveUsers that I'll use to store all
' our user information.
'
' Note: I've wrapped it for readability
Sub Application_OnStart()
	' Selected constants from adovbs.inc
	Const adInteger = 3
	Const adVarChar = 200
	Const adDate = 7
	' Here I set up in memory active user recordset
	' by adding the fields I want to it and defining
	' their data types.
	rstActiveUsers.Fields.Append "id", adInteger
	rstActiveUsers.Fields.Append "ip", adVarChar, 15
	rstActiveUsers.Fields.Append "browser", adVarChar, 255
	rstActiveUsers.Fields.Append "started", adDate
	' Next I open our recordset so that we can use it.
	' That basically gets everything ready for our
	' first user.
	rstActiveUsers.Open
End Sub
Sub Session_OnStart()
	' Set session timeout to 20 minutes
	Session.Timeout = 20
	' Set a session start time.  This is pretty pointless,
	' but it does ensure that we start a session and
	' assign the user a session id and it can help
	' troubleshooting if we ever need it.
	Session("Start") = Now()
	' Move to the end so records are added in order.
	' Again not of any real importance, but it keeps our
	' user table nice and orderly.
	If Not rstActiveUsers.EOF Then rstActiveUsers.MoveLast
	' Add a record and insert users data.  I'm just
	' storing some basic info, but naturally you're free
	' to store whatever you want.
	rstActiveUsers.AddNew
	rstActiveUsers.Fields("id").Value = _
		Session.SessionID
	rstActiveUsers.Fields("ip").Value = _
		Request.ServerVariables("REMOTE_HOST")
	rstActiveUsers.Fields("browser").Value = _
		Request.ServerVariables("HTTP_USER_AGENT")
	rstActiveUsers.Fields("started").Value = _
		Now()
	rstActiveUsers.Update
	' Now that we've got the information, all that's
	' left is to display it.  See test_page.asp for a
	' demo.  It includes the pages show_count.asp and
	' show_users.asp which can also be used
	' individually if desired.
End Sub
Sub Session_OnEnd()
	' Selected constants from adovbs.inc
	Const adSearchForward = 1
	Const adBookmarkFirst = 1
	Const adAffectCurrent = 1
	' Find the appropriate record.  Using session id is the
	' easiest way since I use this as the primary key.
	' This line positions us on the appropriate record.
	rstActiveUsers.Find "id = " & Session.SessionID, _
		0, adSearchForward, adBookmarkFirst
	' Now that we're on the record, delete it.
	' I use the EOF to make sure we've got one.
	If Not rstActiveUsers.EOF Then
		rstActiveUsers.Delete adAffectCurrent
	End If
End Sub
Sub Application_OnEnd()
	' Not like it really matters, but for the sake of
	' good coding practice I close the recordset when
	' our application is shutting down.
	rstActiveUsers.Close
End Sub
</script>

That's all there is to it. I know it's a bit more code then the last article, but it's pretty straight-forward.

Display Routines

The only thing that leaves is the display routines. Show_count.asp just uses the recordcount of the recordset to easily get a currect count. Show_users.asp is a little more code, but should look familiar to anyone who has done any database work. It builds a simple HTML table that displays the data from the recordset. Both can easily be modified to look like your site or to use whatever styling you'd like. As they currently stand they are pretty much plain vanilla HTML. We're coders not designers! ;)

Code Download

The code is available as a zip file. To use it you need to set it up in an ASP application. If you have trouble see the original Counting Active Users and the Applications, Sessions and Global.asa articles listed below.

Update

A number of people have complained of problems where the records have not been getting removed. I tested it on a few machines and haven't been able to reliably replicate it, but in an attempt to try and help resolve the issue you can try and replace the existing Session_OnEnd code with the following sub.

Sub Session_OnEnd()
	' Selected constants from adovbs.inc
	Const adAffectCurrent = 1
	' Start at the first record
	rstActiveUsers.MoveFirst
	' Check each record for the SessionID
	Do While Not rstActiveUsers.EOF
		' Do conversion to Long to be sure we're
		' comparing the same data type.
		If CLng(rstActiveUsers.Fields("id").Value) = _
			CLng(Session.SessionID) Then
			rstActiveUsers.Delete adAffectCurrent
		End If
		rstActiveUsers.MoveNext
	Loop
	rstActiveUsers.Update
End Sub

Related Links


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers