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.
LVL 2
rrattieAsked:
Who is Participating?
 
azadisaryevConnect With a Mentor Commented:
it looks like i did have a typo in my code, sorry... the collection="#session#" should really be collection="#sessionS#" - that's the SESSIONS var you set in your code.

see full sample code attached.
IMPORTANT: my code assumes that you store each logged-in user's name in session.username variable. change that as necessary.

Azadi


<!-- 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.username)>
</cfloop>
<cfoutput>
Logged-in Users (#arrayLen(arrUsers)#): #arrayToList(arrUsers, ", ")#
</cfoutput>

Open in new window

0
 
rrattieAuthor Commented:
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

0
 
azadisaryevCommented:
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
azadisaryevCommented:
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
0
 
rrattieAuthor Commented:
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
0
 
rrattieAuthor Commented:
If I just use the code you posted I get : Variable SESSIONS is undefined.
0
 
rrattieAuthor Commented:
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

0
 
rrattieAuthor Commented:
Odd.. it is working now.  I just changed MM_Username to mm_username and it started working.
0
 
rrattieAuthor Commented:
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.
0
 
azadisaryevCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.