Ask Ben: Testing For ColdFusion Session Management

Posted September 7, 2007 at 8:01 AM

Tags: ColdFusion, Ask Ben

Ben, Do you know how I can test to see if the session scope exists if session management is turned off. I turned off session management and did an isDefined('session') which returned yes. I'm trying to do this before a StructKeyExists(session,'temp'). StructKeyExists throws an exception if the structure doesn't exist, but I don't know how to test for the absence of the session!

At first, I have to say that I didn't believe you; I couldn't believe that the SESSION would be "defined" if ColdFusion session management was turned off. But, after trying it for myself, sure enough, it is defined. In fact, not only is it defined, but you can CFDump it out. It comes up as an empty struct, but it doesn't throw an exception.

To help us in this matter, there is a nice little UNDOCUMENTED function in the ColdFusion APPLICATION scope that allows us to view the application settings. This function, GetApplicationSettings(), is called directly on the APPLICATION scope in dot-notation:

 Launch code in new window » Download code as text file »

  • <!---
  • Dump out ColdFusion application settings. This will
  • give us insight into most of the application-level
  • settings that can be set in the Application.CFM/CFC.
  • --->
  • <cfdump
  • var="#APPLICATION.GetApplicationSettings()#"
  • label="GetApplicationSettings() Output"
  • />

Running the above code, we get the following CFDump output:


 
 
 

 
APPLICATION.GetApplicationSettings() Returns Struct Of Application Level Settings  
 
 
 

As you can see, this returned struct gives us access to most, but not all, of the settings that we can set in the Application.cfm/cfc files. The one that we are most concerned with right now is the SessionManagement key which will give us the boolean value as to whether or not session management has been enabled.

To test this, I have set up a tiny ColdFusion Application.cfc file in which session management has been turned off:

 Launch code in new window » Download code as text file »

  • <cfcomponent
  • output="false"
  • hint="Handles application level events.">
  •  
  • <!--- Define application settings. --->
  • <cfset THIS.Name = "CheckSessionTest" />
  • <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 1, 0 ) />
  • <cfset THIS.SessionManagement = false />
  •  
  • </cfcomponent>

Then, I set up a tiny index.cfm file to test for ColdFusion session management:

 Launch code in new window » Download code as text file »

  • <!---
  • Check to see if Session Management has been enabled
  • in this ColdFusion application.
  • --->
  • <cfif APPLICATION.GetApplicationSettings().SessionManagement>
  •  
  • <p>
  • SESSION Is Enabled
  • </p>
  •  
  • <cfelse>
  •  
  • <p>
  • SESSION Is Disabled
  • </p>
  •  
  • </cfif>

Running the above code, we get the following output:

SESSION Is Disabled

Works quite nicely. Now, like I said before, this is an undocumented feature, so use it at your own discretion. One of the things that I do in some of my applications where ColdFusion Session Management might be toggled on and off is to set a flag in the REQUEST scope, such as REQUEST.HasSessionScope. This way, whatever logic performs the application settings can also set the REQUEST flag.

Hope that helps a bit.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Sep 7, 2007 at 8:22 AM // reply »
56 Comments

Awww Ben, you're the best!


Sep 7, 2007 at 8:24 AM // reply »
6,516 Comments

Any time my man, any time.


Sep 7, 2007 at 11:57 AM // reply »
109 Comments

Instead of using an undocumented function (which means it could be removed or changed at any time), why not just use a try/catch block to try and set a session variable, and create a flag accordingly?


Sep 7, 2007 at 12:02 PM // reply »
6,516 Comments

@Brian,

Good question. People seem to rag on me anytime I used "on purpose" exception throwing to do anything, especially anything that will be done on a regular basis. I guess, the best of all possible worlds would be to encapsulate the function and USE the undocumented features. Then, if they ever get taken out, it only needs to be changed in the function (to use CFTry / CFCatch) and the rest of the code still works.

Also, that way, since exception handling is a bit expensive in terms of processing (or so I am harassed!), this way, you only need to use it when CF changes.


Sep 7, 2007 at 12:03 PM // reply »
42 Comments

Another approach would be to set a session variable, redirect to another page, then check if the variable isDefined().


Sep 7, 2007 at 12:10 PM // reply »
6,516 Comments

@Tom,

True, like testing a cookie value, but that might not be the easiest thing if you are dealing with a single page request or something.


Sep 7, 2007 at 12:10 PM // reply »
56 Comments

The purpose of all this was that upon reiniting my application, I would always get an error because I set the timeout of app and session to be 0 and the page would then process without a session scope.

So, I thought, I'd just put some logic in so that I don't get this error but I couldn't figure out how. It just seemed quirky to me.

Of course, the easy fix for the above is to set the session and application timeout to 1 second upon reinit.


Sep 7, 2007 at 12:15 PM // reply »
6,516 Comments

@Justin,

Yeah, I would go that way. It's probably always easier to have SOME session scope, even if its timeout is very short. Even Michael Dinowitz does that and he is the King of conditional session management.


Sep 7, 2007 at 12:39 PM // reply »
56 Comments

Yeah that's the way I did it, but it's always good to see the other perspective. I always wanted to make sure I wasn't loony about the whole session being defined with sessions turned off thing.


Sep 7, 2007 at 1:16 PM // reply »
97 Comments

I hadn't heard of this before but Googling for it brings up a little more information here (from mid-2005):

http://www.rocketboots.com.au/blog/index.cfm?mode=entry&entry=8A1C48F5-E081-51EF-A7AF8C312529DABA


Sep 9, 2007 at 5:33 PM // reply »
6,516 Comments

Dang! This always happens :) I find out something and then I find out that it was already discussed years ago! Oh well, at least I learned something from this.


Feb 23, 2009 at 6:14 PM // reply »
2 Comments

a little more tricky situation here - i have a view that uses an expression that involves a session scope variable; there is a context in which this view gets called whereby i haven't even determined the application name yet (this.name in the Application.cfc is dynamically determined) so in this context, i cannot access application.GetApplicationSettings() either; i had always though isDefined for either the session scope or the application scope would simply return false if their was no <cfapplication tag or Application.cfc name in play - but alais, its just boots an error - so it seems i have to use the try catch methodology... the good news, this situation is only on a dummy login screen that appears only in our dev environment so the "tax" factor doesnt weigh into our prod environment. does anyone know a non try-catch way to see if an application context has been established yet in the request??


Feb 24, 2009 at 7:53 AM // reply »
6,516 Comments

@Jon,

If the application name isn't even defined at that point, I don't think there is a non-try/catch way as the session management is tied to a given application. And, if it's not defined yet, then I don't think session management is actually defined yet.


Oct 28, 2009 at 3:20 PM // reply »
1 Comments

Hi ben,

I am fairly new to coldfusion.
I am looking to set up a session variable to display Recently viwed items.

I cannot figure out how in Coldfusion.

Can you help me out?


Oct 31, 2009 at 2:20 PM // reply »
6,516 Comments

@Viral,

You can just set the session variables in the OnSessionStart() event of the Application.cfc event handlers. For more information on Application.cfc, check this out:

http://www.bennadel.com/blog/726-ColdFusion-Application-cfc-Tutorial-And-Application-cfc-Reference.htm


Post Comment  |  Ask Ben

Recent Blog Comments
aha
Nov 22, 2009 at 7:42 AM
Using A Name Suffix In ColdFusion's CFMail Tag
Why not? ... read »
Nov 22, 2009 at 7:37 AM
Using A Name Suffix In ColdFusion's CFMail Tag
asd ... read »
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »