msgbartop
News, views, tips and tricks on Oracle and other fun stuff
msgbarbottom

How to track user sessions in ColdFusion

In order to use your ColdFusion web application, a user is required to enter login information, i.e. a user name and a password. Now, you want to know who is logged in at any point in time (maybe you want to shut down the server for maintenance and you do not want any logged in users to lose their work). Here is how you do it:

In your Application.cfm add the following code (make sure you lock all your application variables):

<!---  
If application.sessionTracker is not defined, create it 
and set its initial value to an empty structure
--->    
<cfparam name="application.sessionTracker" 
 default="#StructNew()#">

<!---  
When a user logs in, a session variable, 
session.login_id, is created. So, If we have 
a logged in user then add an entry in the 
sessionTracker with a key of session.login_id 
and a value of the current date/time. The 
time indicates when the last time the user had 
an activity in the application.
--->    
<cfif isDefined ("session.login_id")>
    <cfset dummy = structInsert( 
         application.sessionTracker,
         session.login_id, 
         now(), true)>
</cfif>

That’s it for Application.cfm. All what you did was to record the current user in an application scope variable along with a time stamp of when s/he last visited a page. Now what? Well, your purpose is to know who is logged in to the application and for how long. So, you need to write a new CFM page to report this information. Let’s call the page sessionReport.cfm:

<cfoutput> 
<h3>Live Session Tracker Report</h3> 
<table> 
<tr> 
    <td><b>Login ID</b></td> 
    <td><b>Session Status</b></td> 
</tr> 
<cfloop collection=#application.sessionTracker# 
                  item="auser"> 
    <cfset onlineSince = structfind(
                  application.sessionTracker, auser)> 
    <!--- 
    Assuming that your session timeout is 60 minutes 
    ---> 
    <cfif dateCompare(onlinesince+60, now()) eq 1> 
        <!--- 
        User's last activity lies within session 
             timeout, so his/her session is active 
        ---> 
        <cfset inactiveSince = 
                     datediff("n", onlineSince, Now())> 
        <tr> 
            <td>#aUser#"</td> 
            <td> 
                inactive for <b>#inactiveSince#</b> mins 
            </td> 
        </tr> 
    <cfelse> 
        <!--- 
        User's session has timed out, 
        so we can delete it from the structure 
        ---> 
        <cfset structDelete(
                     application.sessionTracker, aUser)> 
    </cfif> 
</cfloop> 
</table> 
<cfif StructCount(application.SessionTracker) gt 1>
    #StructCount(application.SessionTracker)# 
        Users online. 
</cfif>
</cfoutput> 

And you’re done. Feel free to customize the above to suit your needs.


Filed in ColdFusion on 20 Jun 05 | Tags: ,


Reader's Comments

  1. |

    Does not work. Shows only current user, not all users in CFMX 7.02 on IIS.

  2. |

    It works for me. I also have CFMX 7.02 on IIS.

  3. |

    This is a great script. One question. How would I trigger something to happen when the individual users session variable expires? For instance when the session expires I update a field in a databse to set the loggedin status to 0?

    Any ideas on how I could do that?

  4. |

    Thinking out loud, you can use the same logic in sessionReport.cfm. But instead of creating a report, you update your database. You move the code to Application.cfm or Application.cfc so that it runs before each page.