Determining the ColdFusion Default, and Max, Session timeouts with code

How do you determine the current server’s default and max session timeouts via code? This question came up on Twitter today and I thought I’d whip up a quick example. The answer is relatively simple - make use of the Admin API. Here’s a quick example.

<cfset admin = createObject("component", "CFIDE.adminapi.administrator")> <cfset admin.login("admin")> <cfset runtime = createObject("component", "CFIDE.adminapi.runtime")> <cfset sessionDefaultTimeout = runtime.getScopeProperty("sessionScopeTimeout")> <cfset sessionMaxTimeout = runtime.getScopeProperty("sessionScopeMaxTimeout")> <cfoutput> The default timeout for sessions is #sessionDefaultTimeout# and the max value is #sessionMaxTimeout#. </cfoutput>

This results in (well, on my server anyway):

The default timeout for sessions is 0,0,20,0 and the max value is 2,0,0,0.

Those values are a string where the first number is days, the second is hours, the third is minutes, and the last is seconds. If you need to do math with the numbers, convert it to something real, like minutes:

<!--- convert to minutes ---> <cfset parts = listToArray(sessionDefaultTimeout)> <!--- my total ---> <cfset totalMinutes = 0> <!--- add in days ---> <cfset totalMinutes += 1440 * parts[1]> <!--- add in hours ---> <cfset totalMinutes += 60 * parts[2]> <!--- add in minutes ---> <cfset totalMinutes += parts[3]> <!--- and seconds ---> <cfset totalMinutes += parts[4]/60> <p/> <cfoutput> Total minutes for default sessionTimeout is #totalMinutes#. </cfoutput>

This returns 20 on my server.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA