Skip to main content
February 23, 2011
Answered

coldfusion session variables being lost

  • February 23, 2011
  • 5 replies
  • 18353 views

ok so when my users login I establish session variables. one of these is employee id. in my Application.cfc I have this at the top:

<cfscript>
this.name = "My App";
this.sessiontimeout=CreateTimeSpan("0", "0", "45", "0");
this.SESSIONMANAGEMENT="YES";
this.SetClientCookies = "true";
this.setDomainCookies = "true";
</cfscript>

i also set the timeout for sessions at 45 in the CFadmin too.

after a successfull login i capture their session.employeenumber by authenticating through our corporate LDAP. no problem there. note: this app has run smoothly for 6+ years now but something has recently gone wrong.

on every page i reference a file in my application directory called "check_session.cfm" like this:

<cfmodule template="/myApp/Tags/check_session.cfm">

In that file I have code that checks for the existence of the session.employee_number. if it doesn't exist they either timed out or book-marked a page in the app in which they need to be logged in to view:

<cflock timeout="30" throwontimeout="Yes" type="READONLY" scope="SESSION">
<!--- Checks to make sure the user has a session--->
<CFIF NOT IsDefined("Session.employee_number")>
     <CFLOCATION URL="http://#cgi.HTTP_HOST#/myApp/LoginAgain.cfm">
</CFIF>
</cflock>

every now and then (i'd say on average 5 times per week) i get an error message from a user (i re-direct all errors to my email with diagnostic info). the error reads "Element EMPLOYEE_NUMBER is undefined in SESSION". when i look at the page the error came from i see my call to the check_session.cfm file about 50 lines above where this error happened when i tried to reference session.employee_number. why wouldn't it not find it and re-direct the user to the timeout page? i can never duplicate this error on my machine either... i always get the right result without the error message. its a random, sporadic, intermittent error.

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    A long time ago (but in this galaxy) we found isDefined() can give false positives with session variables, in exactly the fashion you describe.  Or indeed something as simple as this could error with "session.foo does not exist" on line 2 (ie: the line inside the if().

    if (isDefined("session.foo")){

         something = session.foo;

    }

    And the session variable absolutely DID NOT exist (nor did a similar variable exist in any of the scopes isDefined() checks, eg: variables.session.foo).  So the error was right... but the isDefined() call wasn't working.

    The problem was occasional, so difficult to track.

    We changed the calls to structKeyExists() and the problem went away.

    This would have been back in CFMX7 though, and we never really spent much time investigating it further after we treated the symptom.  I've been aware of others with similar experiences though.

    However we stopped using isDefined() at that point (for everything), and never had similar problems ever again.

    Maybe try it.

    --

    Adam

    5 replies

    Inspiring
    February 24, 2011

    I am having a similar problem but I am using the following code to try and capture a session that has timed out. If the session scope is less that 25 then I know the user is not logged in.

    Even though I have this code on the Application.cfm page I still get errors like, "Element FOO is undefined in SESSION."

    <cfif NOT isDefined("session") OR (isDefined("session") and session.size() LT 25)>

        <cflocation url="/logout.cfm">
    </cfif>

    I am going to see if structkeyexists fixes this like Adam suggests?

    February 24, 2011

    Hi,

    Can you elaborate the error message so that I can get more informations regarding the error and suggest you a suitable solution if possible.

    Thanks,

    Sonu

    February 24, 2011

    Here is the diagnostic error information. Where you see [omitted] i've changed the URL for the purposes of displaying this detail in a public forum:

    struct

    Browser

    Mozilla/4.0   (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR   2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;   InfoPath.2; .NET4.0C; .NET4.0E; MS-RTC LM 8)

    DateTime

    {ts   '2011-02-21 20:59:21'}

    Diagnostics

    Element   EMPLOYEENUMBER is undefined in SESSION. <br>The error occurred on line   162.

    GeneratedContent

    <!DOCTYPE   html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html xmlns="http://www.w3.org/1999/xhtml">   <!-- DW6 --> <head> <!-- Copyright 2005 Macromedia, Inc. All   rights reserved. --> <title>My Home</title> <meta   http-equiv="Content-Type" content="text/html;   charset=iso-8859-1" /> <link rel="stylesheet"   href="mm_training.css" type="text/css" /> <link   rel="stylesheet" type="text/css" href="../template/organized.css">   </head> <body bgcolor="#ffffff"   onLoad="onInit()">

    HTTPReferer

    http://[omitted]/mypage/myhome.cfm?CFID=101686&CFTOKEN=53097237

    Mailto

    [empty   string]

    Message

    Element   EMPLOYEENUMBER is undefined in SESSION.

    QueryString

    [empty   string]

    RemoteAddress

    10.x.x.x   [remote address omitted for purposes of this forum]

    RootCause

    struct

    Detail

    [empty     string]

    ErrNumber

    0

    Message

    Element     EMPLOYEENUMBER is undefined in SESSION.

    Resolvedname

    SESSION

    StackTrace

    coldfusion.runtime.UndefinedElementException:     Element EMPLOYEENUMBER is undefined in SESSION. at     coldfusion.runtime.CfJspPage.resolveCanonicalName(CfJspPage.java:1659) at     coldfusion.runtime.CfJspPage._resolve(CfJspPage.java:1612) at     coldfusion.runtime.CfJspPage._resolveAndAutoscalarize(CfJspPage.java:1747)     at     coldfusion.runtime.CfJspPage._resolveAndAutoscalarize(CfJspPage.java:1740)     at cfmyhome2ecfm1641837815._factor14(D:\[path_omitted]\mypage\myhome.cfm:162)     at cfmyhome2ecfm1641837815.runPage(D:\[path_omitted]\mypage\myhome.cfm:1)     at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:196) at     coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:370) at     coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65) at     coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:273) at     coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)     at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40) at     coldfusion.filter.PathFilter.invoke(PathFilter.java:86) at     coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70) at     coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)     at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) at     coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46) at     coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) at     coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22) at     coldfusion.CfmServlet.service(CfmServlet.java:175) at     coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89) at     jrun.servlet.FilterChain.doFilter(FilterChain.java:86) at     coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)     at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)     at jrun.servlet.FilterChain.doFilter(FilterChain.java:94) at     jrun.servlet.FilterChain.service(FilterChain.java:101) at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)     at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42) at     jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)     at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)     at     jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)     at     jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:320)     at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)     at     jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:266)     at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

    TagContext

    array

    1

    struct

    COLUMN

    0

    ID

    ??

    LINE

    162

    RAW_TRACE

    at         cfmyhome2ecfm1641837815._factor14(D:\[path_omitted]\mypage\myhome.cfm:162)

    TEMPLATE

    D:\[path_omitted]\mypage\myhome.cfm

    TYPE

    CFML

    2

    struct

    COLUMN

    0

    ID

    CF_MYHOME

    LINE

    1

    RAW_TRACE

    at         cfmyhome2ecfm1641837815.runPage(D:\[path_omitted]\mypage\myhome.cfm:1)

    TEMPLATE

    D:\[path_omitted]\mypage\myhome.cfm

    TYPE

    CFML

    Type

    Expression

    element

    EMPLOYEENUMBER

    objectType

    object of java.lang.Class

    Class       Name

    java.lang.Class

    Methods

    Method

    Return Type

    asSubclass(java.lang.Class)

    java.lang.Class

    cast(java.lang.Object)

    java.lang.Object

    desiredAssertionStatus()

    boolean

    forName(java.lang.String,         boolean, java.lang.ClassLoader)

    java.lang.Class

    forName(java.lang.String)

    java.lang.Class

    getAnnotation(java.lang.Class)

    java.lang.annotation.Annotation

    getAnnotations()

    java.lang.annotation.Annotation[]

    getCanonicalName()

    java.lang.String

    getClassLoader()

    java.lang.ClassLoader

    getClasses()

    java.lang.Class[]

    getComponentType()

    java.lang.Class

    getConstructor(java.lang.Class[])

    java.lang.reflect.Constructor

    getConstructors()

    java.lang.reflect.Constructor[]

    getDeclaredAnnotations()

    java.lang.annotation.Annotation[]

    getDeclaredClasses()

    java.lang.Class[]

    getDeclaredConstructor(java.lang.Class[])

    java.lang.reflect.Constructor

    getDeclaredConstructors()

    java.lang.reflect.Constructor[]

    getDeclaredField(java.lang.String)

    java.lang.reflect.Field

    getDeclaredFields()

    java.lang.reflect.Field[]

    getDeclaredMethod(java.lang.String,         java.lang.Class[])

    java.lang.reflect.Method

    getDeclaredMethods()

    java.lang.reflect.Method[]

    getDeclaringClass()

    java.lang.Class

    getEnclosingClass()

    java.lang.Class

    getEnclosingConstructor()

    java.lang.reflect.Constructor

    getEnclosingMethod()

    java.lang.reflect.Method

    getEnumConstants()

    java.lang.Object[]

    getField(java.lang.String)

    java.lang.reflect.Field

    getFields()

    java.lang.reflect.Field[]

    getGenericInterfaces()

    java.lang.reflect.Type[]

    getGenericSuperclass()

    java.lang.reflect.Type

    getInterfaces()

    java.lang.Class[]

    getMethod(java.lang.String,         java.lang.Class[])

    java.lang.reflect.Method

    getMethods()

    java.lang.reflect.Method[]

    getModifiers()

    int

    getName()

    java.lang.String

    getPackage()

    java.lang.Package

    getProtectionDomain()

    java.security.ProtectionDomain

    getResource(java.lang.String)

    java.net.URL

    getResourceAsStream(java.lang.String)

    java.io.InputStream

    getSigners()

    java.lang.Object[]

    getSimpleName()

    java.lang.String

    getSuperclass()

    java.lang.Class

    getTypeParameters()

    java.lang.reflect.TypeVariable[]

    isAnnotation()

    boolean

    isAnnotationPresent(java.lang.Class)

    boolean

    isAnonymousClass()

    boolean

    isArray()

    boolean

    isAssignableFrom(java.lang.Class)

    boolean

    isEnum()

    boolean

    isInstance(java.lang.Object)

    boolean

    isInterface()

    boolean

    isLocalClass()

    boolean

    isMemberClass()

    boolean

    isPrimitive()

    boolean

    isSynthetic()

    boolean

    newInstance()

    java.lang.Object

    toString()

    java.lang.String

    StackTrace

    coldfusion.runtime.UndefinedElementException:   Element EMPLOYEENUMBER is undefined in SESSION. at coldfusion.runtime.CfJspPage.resolveCanonicalName(CfJspPage.java:1659)   at coldfusion.runtime.CfJspPage._resolve(CfJspPage.java:1612) at   coldfusion.runtime.CfJspPage._resolveAndAutoscalarize(CfJspPage.java:1747) at   coldfusion.runtime.CfJspPage._resolveAndAutoscalarize(CfJspPage.java:1740) at   cfmyhome2ecfm1641837815._factor14(D:\[path_omitted]\mypage\myhome.cfm:162) at   cfmyhome2ecfm1641837815.runPage(D:\[path_omitted]\mypage\myhome.cfm:1) at   coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:196) at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:370)   at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65) at   coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:273) at   coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)   at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40) at   coldfusion.filter.PathFilter.invoke(PathFilter.java:86) at   coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70) at   coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)   at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38) at   coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46) at   coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38) at   coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22) at   coldfusion.CfmServlet.service(CfmServlet.java:175) at   coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89) at   jrun.servlet.FilterChain.doFilter(FilterChain.java:86) at   coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)   at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46) at   jrun.servlet.FilterChain.doFilter(FilterChain.java:94) at jrun.servlet.FilterChain.service(FilterChain.java:101)   at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106) at   jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42) at   jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286) at   jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543) at   jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)   at   jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:320)   at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)   at   jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:266)   at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)

    TagContext

    array

    1

    struct

    COLUMN

    0

    ID

    ??

    LINE

    162

    RAW_TRACE

    at       cfmyhome2ecfm1641837815._factor14(D:\[path_omitted]\mypage\myhome.cfm:162)

    TEMPLATE

    D:\[path_omitted]\mypage\myhome.cfm

    TYPE

    CFML

    2

    struct

    COLUMN

    0

    ID

    CF_MYHOME

    LINE

    1

    RAW_TRACE

    at       cfmyhome2ecfm1641837815.runPage(D:\[path_omitted]\mypage\myhome.cfm:1)

    TEMPLATE

    D:\[path_omitted]\mypage\myhome.cfm

    TYPE

    CFML

    Template

    /[omitted]/mypage/myhome.cfm

    Type

    coldfusion.runtime.CfErrorWrapper

    Participating Frequently
    February 25, 2011

    Have it do a dump of the session scope. Where do you set that

    EMPLOYEENUMBER variable?

    Is it possible the session is timing out, then someone sits down and clicks

    on another page alredy in your protected area. Thus starting a new session,

    but not triggering your code where the EMPLOYEENUMBER var is set?

    BKBK
    Community Expert
    Community Expert
    February 24, 2011

    Two things. Firstly, as someone said earlier, remove the space from the application name. Secondly, it is very likely the session wasn't lost, but had ended.

    If so, then you should look at how to handle the case when a session ends. Some people do so by setting default session variables using <cfparam>. Then the session variable will always exist. Others use <cfif isDefined("session.myVar") to test for existence in code that reads the session variable.

    February 24, 2011

    Hi BKBK,

    The app name is 1 word with no spaces... for the purposes of this post I added in the "My App" so this wouldnt be the issue. Also (as I put in my first post) I am doing the session check to see if it has ended.

    The strange thing is that on line 1 of my page I reference a CFM file by using the <cfmodule template="mypage.cfm"> which does

    (as you mentioned) check for the existence of the session variable by using the IsDefined function. So if it ended it should have redirected the user to the timed out page with a link to re-login. However this line of code ran fine and then on the same page down at line 500 where I am writing code to reference the session variable needed I'm then getting the "Element [my variable name here] is Undefined in Session". why wasnt this caught at line 1 then?

    Adam Cameron.Correct answer
    Inspiring
    February 24, 2011

    A long time ago (but in this galaxy) we found isDefined() can give false positives with session variables, in exactly the fashion you describe.  Or indeed something as simple as this could error with "session.foo does not exist" on line 2 (ie: the line inside the if().

    if (isDefined("session.foo")){

         something = session.foo;

    }

    And the session variable absolutely DID NOT exist (nor did a similar variable exist in any of the scopes isDefined() checks, eg: variables.session.foo).  So the error was right... but the isDefined() call wasn't working.

    The problem was occasional, so difficult to track.

    We changed the calls to structKeyExists() and the problem went away.

    This would have been back in CFMX7 though, and we never really spent much time investigating it further after we treated the symptom.  I've been aware of others with similar experiences though.

    However we stopped using isDefined() at that point (for everything), and never had similar problems ever again.

    Maybe try it.

    --

    Adam

    Legend
    February 23, 2011

    I think the application name must be in the format of a valid variable name. I have not idea if this is your issue as I have not encountered this.

    12Robots
    Participating Frequently
    February 23, 2011

    Does your site use html frames?

    February 23, 2011

    no my site does not use iframes