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:
<!--- 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:
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:
<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:
<!--- 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.
Want to use code from this post? Check out the license.