Solved

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

Posted on 2009-05-18
10
308 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:rrattie
  • 6
  • 4
10 Comments
 
LVL 2

Author Comment

by:rrattie
ID: 24414926
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24415264
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24415391
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:rrattie
ID: 24422038
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
 
LVL 2

Author Comment

by:rrattie
ID: 24422060
If I just use the code you posted I get : Variable SESSIONS is undefined.
0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 500 total points
ID: 24428343
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
 
LVL 2

Author Comment

by:rrattie
ID: 24432131
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
 
LVL 2

Author Comment

by:rrattie
ID: 24432164
Odd.. it is working now.  I just changed MM_Username to mm_username and it started working.
0
 
LVL 2

Author Closing Comment

by:rrattie
ID: 31582702
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
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24432288
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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to access an read a file outside of the web root. 5 43
Update the query a CFSELECT uses when a checkbox is checked 3 69
paging 3 46
ajaxSubmit is giving me an error 1 47
This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question