Maintaining a Session

On the CF-Talk list, Richard Colman asked how he could keep a session alive for longer than the 30 minute limit imposed by Crystal Tech.

Michael Dawson provided a very good answer (quoted below):


I setup my intranet so that, even if the session times-out, it will be transparent to the end-user. I do this by keeping a separate session cookie.

When the session times-out, then the user accesses another page in the application, my scripts will reload the data in the session scope again. The end-users have not complained about this process.

In addition to that approach, I would suggest the following bit of JavaScript:
function keepAlive() {
var imgAlive = new Image();
var d = new Date();
imgAlive.src = '/alive.cfm?d=' + d;
}
setInterval("keepAlive()", 20*60*1000);

Before the script is added to your site, add a small alive.cfm file to the root of your site (name it whatever you wish, just be consistent). The script makes sure that a page is requested from your ColdFusion server by their browser every 20 minutes (safely less than the 30 minute time-out). Alive.cfm need not return an image - JavaScript doesn't check.

Keep in mind that this only works for users who have JavaScript enabled, so make sure to use this technique in conjuction with (not in place of) the approach suggested by Michael Dawson.

This technique actually has a great many uses that I will try to post in future entries. I have used it in the past to change database data without a form submit and also for JavaScript error handling.

Update!

Martin Perry added this suggestion to the discussion to get around the JavaScript issue:
Alternatively, to get around the JavaScript bit, create an invisible IFRAME which has it SRC set to the alive.cfm.. On Alive.cfm, add a HTTP redirect to redirect back to itself after 1200 seconds.

<META HTTP-EQUIV="Refresh" CONTENT="1200;URL=/alive.cfm?cachebuster=<cfoutput>#createUUID()#</cfoutput>">

I never think about IFRAME myself (trying to remain standards compliant). Nevertheless, it is an excellent suggestion. Good call Martin!

Unobtrusive Javascript

I was reading through Bill Rawlinson's blog and in two different entries he mentions Unobtrusive Javascript. I had to check it out.

After reading about it, I have to say that I am thoroughly amazed. These are thoughts that I had been having about JavaScript in a general sense, but taken to a level that I hadn't yet reached (or, let's be honest, gotten very close to). From now on, my goal in writing JavaScript will be to meet those guidelines.

In short, Unobtrusive JavaScript is an an attempt to separate functionality from HTML so that functionality doesn't polute the mark-up. This is for essentially the same purpose as separating styling from HTML. It should also make sure not to hinder a page from working when JavaScript isn't available or get in the way of accessibility.

Read about Unobtrusive Javascript.

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.