Solved

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

Posted on 2009-05-18
10
304 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now