[thelist] number of sessions stored on a Coldfusion server - newbie question ?

Raymond Camden jedimaster at macromedia.com
Fri Jan 18 08:39:20 CST 2002


There is no way to get the sessions currently active. (Although, trust
me, I've been asking for it for some time. ;)

If all you need is the # of sessions, there is a very easy way to do it.

Simply add this to your application.cfm file:

<cflock scope="session" type="ReadOnly" timeout=3>
	<cfset token = session.urltoken>
</cflock>
<cflock scope="application" type="exclusive" timeout=3>
	<cfparam name="application.sessions" default="#structNew()#">
	<cfset application.sessions[token] = now()>
</cflock>

This simply copies the current time to an application structure. It uses
your session.urltoken, a unique identifier, as the key. What you will
have is a structure that contains all the sessions and the last time
they hit the site. However, it will also contain sessions that have
timed out. So, all you have to do when you display the # of sessions is
loop through the struct, and if the datadiff between now and their last
hit is > your sessiontime (defaults to 20 mins), then you simply nuke
the key. You can then just output: #structcount(application.sessions)#.

=======================================================================
Raymond Camden, Principal Spectra Compliance Engineer for Macromedia

Email    : jedimaster at macromedia.com
Yahoo IM : morpheus

"My ally is the Force, and a powerful ally it is." - Yoda 

> -----Original Message-----
> From: thelist-admin at lists.evolt.org 
> [mailto:thelist-admin at lists.evolt.org] On Behalf Of Gilles Vincent
> Sent: Friday, January 18, 2002 9:27 AM
> To: thelist at lists.evolt.org
> Subject: [thelist] number of sessions stored on a Coldfusion 
> server - newbie question ?
> 
> 
> Hello,
> I've found an excellent tag to track users on
> http://cfhub.com/tutorials/online/ (the <cf_trackman> is 
> copied below) ;
> however, I would like to get something less complicated : I 
> only need the
> number of sessions that are stored on the server :
> is there any (non documented ?) server variable that can give 
> it (maybe in
> ClusterCats?), or does I always need to store them "manually" in an
> application var like in trakman.cfm?
> Gilles Vincent
> 
> Tip : <cf_trackman> from cfhub.com
> <!---
> PARAMETERLIST
> 
> Action
>   -ShowAll    (Default) Builds a list of all the logged in
>               users, their page, and their last "Active"
>               time.
>   -ShowPage   Builds a list of all users "on the same page"
>   -ShowDir    Builds a list of users in "this" folder
>               (like for a forum maybe?)
> 
> Display
>   -Yes        (Default) Uses the "hard coded" display code
>   -No         Stores the information in a "Request" scoped
>               list: Request.MemberList
> --->
> 
> <!---===========================================
> Code between these sets of horizontal bars
> could be cut/pasted into your Application.cfm
> file.  The application may run a hair quicker.
> =============================================--->
> 
> <!---Does the Application.Online variable exist yet?--->
> <cflock name="TrackMan" type="ReadOnly" timeout="5">
>   <cfif IsDefined("Application.Online")>
>     <cfset CreateStructure=FALSE>
>   <cfelse>
>     <cfset CreateStructure=TRUE>
>   </cfif>
> </cflock>
> 
> <!---Create Application.Online (once only)--->
> <cfif CreateStructure>
>   <cflock name="TrackMan" type="Exclusive" timeout="5">
>     <cfset Application.Online=StructNew()>
>   </cflock>
> </cfif>
> 
> <!---Remove "old" users ("TimeOut Minutes")--->
> <!---First build a list of users "older" than "TimeOut"--->
> <cfset TimeOut=30>
> <cfset UserList="">
> <cflock name="TrackMan" type="ReadOnly" timeout="10">
>   <cfloop collection="#Application.Online#" item="ThisUser">
>     <cfif DateDiff("n",Application.Online[ThisUser][1],now()) 
> GTE TimeOut>
>       <cfset UserList=ListAppend(UserList,ThisUser)>
>     </cfif>
>   </cfloop>
> </cflock>
> 
> <!---Then delete "old" users--->
> <cfloop list="#UserList#" index="ThisUser">
>   <cflock name="TrackMan" type="Exclusive" timeout="5">
>     <cfset Temp=StructDelete(Application.Online,ThisUser)>
>   </cflock>
> </cfloop>
> 
> 
> <!---Build "This" Members "Info" Array--->
> <cfscript>
>   Members=ArrayNew(1);
>   Members[1]=now();
>   Members[2]=CGI.CF_Template_Path;
> </cfscript>
> 
> <!---add Members "Info" to "Online" Structure--->
> <cflock name="TrackMan" type="Exclusive" timeout="5">
>   <cfset 
> Temp=StructInsert(Application.Online,Session.Admin,Members,TRUE)>
> </cflock>
> 
> <!---===========================================
> If you like, paste all the preceding code into your
> Application.cfm file.
> =============================================--->
> 
> 
> <!---The tag parameters--->
> <cfparam name="Attributes.Action" default="ShowAll">
> <cfparam name="Attributes.Display" default="No">
> 
> <!---The main "Switch"--->
> <cfswitch Expression="#Attributes.Action#">
> 
> <!---Build List of Users on the same page--->
>   <cfcase value="ShowPage">
>    <cfset Request.MemberList="">
>    <cflock name="TrackMan" type="ReadOnly" timeout="10">
>      <cfloop collection="#Application.Online#" item="ThisUser">
>       <cfif Application.Online[ThisUser][2] IS CGI.CF_Template_Path>
>         <cfset 
> Request.MemberList=ListAppend(Request.MemberList,ThisUser)>
>       </cfif>
>      </cfloop>
>    </cflock>
>   </cfcase>
> 
> <!---Build List of Users in the same folder--->
>   <cfcase value="ShowDir">
>     <cfset Request.MemberList="">
>     <cflock name="TrackMan" type="ReadOnly" timeout="10">
>       <cfloop collection="#Application.Online#" item="ThisUser">
>        <cfif GetDirectoryFromPath(Application.Online[ThisUser][2])
>              IS
>              GetDirectoryFromPath(CGI.CF_Template_Path)>
>         <cfset 
> Request.MemberList=ListAppend(Request.MemberList,ThisUser)>
>        </cfif>
>       </cfloop>
>     </cflock>
>   </cfcase>
> 
> <!---Default (all logged in users)--->
>   <cfdefaultcase>
>     <cfset Request.MemberList="">
>     <cflock name="TrackMan" type="ReadOnly" timeout="10">
>       <cfloop collection="#Application.Online#" item="ThisUser">
>         <cfset 
> Request.MemberList=ListAppend(Request.MemberList,ThisUser)>
>       </cfloop>
>     </cflock>
>   </cfdefaultcase>
> </cfswitch>
> 
> 
> <!---Display the information if Display="Yes"
>      Obviously a little table formatting would go a
>      long way here... This table is a little "simple".
>      Some pretty colours perhaps?--->
> 
> <cfif Attributes.Display is "Yes">
>   <table border=2 rules="All">
>     <cflock name="TrackMan" type="ReadOnly" timeout="20">
>     <cfloop list="#Request.MemberList#" index="ThisUser">
>       <cfoutput>
> 
>         <tr><td>
>         <b>#ThisUser#</b>
>           is currently viewing page
>         <b>#Application.Online[ThisUser][2]#</b>
>           and has been inactive since
>         <b>#TimeFormat(Application.Online[ThisUser][1])#</b>
>         <b>#DateFormat(Application.Online[ThisUser][1])#</b>
>         </td></tr>
> 
>       </cfoutput>
>     </cfloop>
>     </cflock>
>   </table>
> </cfif>
> 
> 
> 
> -- 
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt ! 
> 





More information about the thelist mailing list