Managing Sessions In CF Ajax Applications

Everyone is likely familiar with standard application logic for managing authenticated sessions with ColdFusion. Typically you do some sort of check inside your Application.cfc file (usually in onRequestStart()) and if the current user is not authenticated (or there session has timed out) you redirect them to a login screen. Nothing new there, but when you think of the nature of an Ajax application you realize that there is a fundamental issue with that concept. What happens if a cfdiv were to be refreshed and the users session has timed out? Well, likely your cfdiv would then contain the results of the redirection (maybe a login.cfm template), but that may not be the 'prettiest' solution depending on your application.

So I put together a quick application to demo one possible solution. The basic concept this:

  • Log a timestamp of the last 'hit' by a given user in their session
  • Create a simple facade that looks at the time difference between the last hit and the current time
  • Create a ajax proxy to query that session facade
  • Make sure the proxy request does not update the 'last hit' - otherwise your session will never time out
  • Run a call to the session facade (via the ajax proxy) on a set interval
  • Evaluate the inactivity period, if in a 'warning' period (say between 15-19 minutes) display a modal window warning the user.
  • If the user chooses to extend the session, ping the session facade which will update the 'last hit' variable.
  • If the inactivity is beyond the session timeout (I usually go with >= 19 minutes display a modal (non-closable) window telling them the session has timed out

I have a demo online here. If you open the demo and wait longer then a minute you'll be warned. Longer then 2 minutes and you're logged out. Full source is attached to this entry. As always, feel free to post questions/comments.

Comments
Dude - You ROCK!
# Posted By Bruce | 4/9/08 7:38 PM
Thank you sir :) Glad to help.
# Posted By todd sharp | 4/10/08 9:45 AM
Sweet work Todd!

During the last weeks (and coming weeks) I'm working on a application using the CFajax stuff exclusivly. Thanks to the blogging of you, Dan Vega, Ray Camden and a few others the process have been easier than it would have been leaning on the CF docs only.

What you are showing of here is something that was on my "to solve" list. Excelent!!!

Thanx a lot for this, and your other posts on the topic.
# Posted By Trond Ulseth | 4/16/08 11:40 AM
that's a lot of ping's going back and forth.

on my last cf ajax app, I made use of onbinderror. Just use cfheader to send back an error code when session expired.
# Posted By Henry | 4/18/08 7:37 AM

Calendar

Sun Mon Tue Wed Thu Fri Sat
  12345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex misc model-glue off topic personal project learn slidesix sql

Recent Comments

More CF+Java: Compiling Classes And Persisting Objects
Getburl said: I have been attempting to get Db4o working in my CF application and I have not succeeded. I would lo... [More]

Thoughts On Ajax Frameworks And ColdFusion/Adobe
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

Extending Ext With Ext Extensions
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

CF Needs An Open Source Contact List Importer
Kay Smoljak said: Heh, the fact that sites DO it doesn't mean they SHOULD. To us it's ok, but to a non-tech-savvy user... [More]

A Few Project Updates
Helena said: Now punctually what is the situation ? [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com