Forum Moderators: open

Message Too Old, No Replies

Active users count

how does that work?

         

too much information

2:05 am on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have seen several forums online where there is a count at the bottom that tells how may visitors are on that particular thread at the moment. (or at least when the page loaded)

I can imagine that adding to the count would be very simple, but how do you know when to take people out of the count.

Is this just a simple session variable thing or is this database related? Any ideas where to find tips on setting this up?

too much information

4:08 am on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I solved that problem. It's now a neat toy. Here's the tricky part:

What I'm trying to do is show my subscribers (the site is a cart system) how many active visitors are looking at their different product listings. So when they look at their list of categories they can see how many people are online looking right now.

Seems simple except I have no control over how many categories they create and what they name them. I'm a very flexible guy. ;) So how can I dynamically tag each session depending on what each event is called? (That's the one thing that must be unique across the system)

txbakers

2:45 pm on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In ASP there is a routine in the global.asa file which tracks open sessions. I use it, but it's not very accurate as sessions linger.

It gives me a general idea of how many people are currently on the system, but it's not perfect. I can post the code for it if you want.

it's in VBScript for ASP.

macrost

6:17 pm on Feb 6, 2004 (gmt 0)

10+ Year Member



txbakers,
Could I get a glimpse of that code?
Thanks,
Mac

too much information

7:21 pm on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try a Google search for "ASP count active users" that's where I found a great script.

I'm trying to see if I can now separate the count depending on which page they are on. But I need it to be dynamic because I don't have complete control over the existance of pages on the site.

Think of it like a forum. If a user creates a new thread, how do I tell the global.asa that it needs to keep a count on that page as well?

txbakers

7:54 pm on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This might be the same one you found, but I modified it just a tad:

<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 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


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>

Then I have a simple ASP page which reads and displays those variables.

too much information

2:00 am on Feb 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well here's a problem I just noticed. Some spyware will hit your site from different IPs or even from the same IP and it won't show in your logs. However, this little script does pick them up and show them as active users. Could be a nice feature if people think there are more visitors than there really are.

Then again, it looks like those spyware goons have burst my bubble a bit.

(My script is very similar but about 1/2 that size. I haven't had a chance to modify it yet.)