Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-18
10
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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