Link to home
Start Free TrialLog in
Avatar of rrattie
rrattieFlag for United States of America

asked on

How to determine the number of people currently on my site?

I would like to have a place in the admin backend of my site that tells me the approximate number of people currently on the site when I login to the backend. There is no frontend login so not concerned with that, just those who are browsing the site.
Avatar of rrattie
rrattie
Flag of United States of America image

ASKER

Okay I figured out how to get a number of cumulative  number or active sessions (whether logged in or now).  But I would also like to have a list of any and all logged in users on the backend.  Have it coded to show the current user (me) is logged in, but if someone else is logged in it doesn't show them unless I do a dump of the session.

<!-- to show how many sessions are active -->
<cfset tracker = createObject("java","coldfusion.runtime.SessionTracker")>
<cfoutput><p>There are #tracker.getSessionCount()# active sessions</p></cfoutput>
 
<!-- to show the details of the active sessions -->
<cfset tracker = createObject("java","coldfusion.runtime.SessionTracker")>
<cfset sessions = tracker.getSessionCollection(application.applicationName)>
<cfdump var="#sessions#">

Open in new window

if your frontend and backend run under same application scope (share the same application), then you could set some application-scope variable and increase it by 1 on user's session start (and decrease it by 1 on user's session end). this app-scope variable can give you a rough idea of how many people are on your site. the roughness will depend on your session timeout settings and if you implement some spiders/bots-specific session management or not - read more on this in ben nadel's blog: http://www.bennadel.com/blog/1083-ColdFusion-Session-Management-And-Spiders-Bots.htm.

if your frontend and backend are separate applications (they do not share same application scope), then you probably better set up a db table with just one column and increase/decease that column's value by 1 on user's session start/end. make sure you re-set that column to 0 on your frontend application's start.

an alternative to storing session count in a db table would be to implement an application proxy cfc. read this sean's blog post for more info: http://corfield.org/entry/Extending_Your_Root_Applicationcfc

for either of methods you will need to use Application.cfc, so that you have access to onApplicationStart(), onSessionStart() and onSessionEnd() methods.

if you are new to cf, all of the above may be well over your head, but if you have worked with CFCs and used Application.cfc then you should be able to do this. if you have never used Application.cfc, ben nadel has a nice round-up of it: http://www.bennadel.com/blog/726-ColdFusion-Application-cfc-Tutorial-And-Application-cfc-Reference.htm

Azadi
ok, i see you know very well what you are doing... :)

if you store user's data in session scope (i.e. user's name, id, etc), you can probably get that data by looping over your #sessions# collection...

<cfset users = []>
<cfloop collection="#session#" item="curSession">
<cfset thisSession = sessions[curSession]>
<cfset arrayappend(users, thisSession.username)>
</cfloop>
<cfdump var="#users#">

Azadi
Avatar of rrattie

ASKER

Sometimes I know what I'm doing and other times my brain refuses to work.

Tried looping and got this.

Element mm_userauthorization is undefined in a Java object of type class coldfusion.runtime.AppSessionCollection
Avatar of rrattie

ASKER

If I just use the code you posted I get : Variable SESSIONS is undefined.
ASKER CERTIFIED SOLUTION
Avatar of azadisaryev
azadisaryev
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rrattie

ASKER

I'm getting an the error "Element MM_username is undefined in THISSESSION"  (MM_username is the variable their username is stored in).
<!--- Here is the code from the login page that sets the variables --->
<cflock scope="Session" timeout="30" type="Exclusive">
      <cfset Session.MM_Username=FORM.username>
      <cfset Session.MM_UserAuthorization=MM_rsUser.access_level[1]> 
	  <cfset Session.MM_Email=MM_rsUser.email_address>
	  <cfset Session.MM_Name=MM_rsUser.name>
    </cflock>
 
 
<!--- The code with my var inserted --->
<!--- to show how many sessions are active --->
<cfset tracker = createObject("java", "coldfusion.runtime.SessionTracker")>
<cfoutput><p>There are #tracker.getSessionCount()# active sessions</p></cfoutput>
 
<!--- create SESSIONS collection --->
<cfset sessions = tracker.getSessionCollection(application.applicationName)>
 
<!--- create USERS array and populate with each logged-in user's name from session.username variable in each user's session --->
<cfset arrUsers = arrayNew(1)>
<cfloop collection="#sessions#" item="curSession">
  <cfset thisSession = sessions[curSession]>
  <cfset arrayAppend(arrUsers, thisSession.MM_Username)>
</cfloop>
<cfoutput>
Logged-in Users (#arrayLen(arrUsers)#): #arrayToList(arrUsers, ", ")#
</cfoutput>

Open in new window

Avatar of rrattie

ASKER

Odd.. it is working now.  I just changed MM_Username to mm_username and it started working.
Avatar of rrattie

ASKER

Thanks for your help!  I always can count on EE to help me out when I'm working on too many projects at once and I'm missing something.
that IS odd... maybe some Java-specific casing? never saw that before... but then i usually use all-lowercase variables with underscores instead of mixed-case/camel-case ones...

Azadi