Skip to main content
September 28, 2014
Question

ColdFusion 11 Throws Error Session Variable Undefined, When in fact it is Defined as shown by CFDump.

  • September 28, 2014
  • 1 reply
  • 6042 views

I've been having a particularly annoying error with ColdFusion 11, and unable to track down the source of this problem.

Basically There are a few variables defined in the session scope we check with isdefined a few lines above to make sure they are defined. Subsequent lines may or may not throw a #variablename# is undefined on random lines.

What can I do to fix this issue? Is this a known bug?

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
September 28, 2014

I am unaware of such a session issue. Could you show us a code sample?

Make sure your isDefined test applies to all the session variables? That is, your test should be equivalent to the following

<cfif isDefined("session.var1") and isDefined("session.var2") and isDefined("session.var3") etc>

<!--- session.var1, session.var2, session.var3, ... all exist --->

</cfif>

September 29, 2014

I am doing isDefined checks for relevant variables when neccessary. The problem is not that the code errors out undefined ... it's that the variable is actually defined and errors out undefined.


As a simplified example;

Login.cfm Form supplies form.username and form.password to login_action.cfm then redirects to index.cfm after succesful LDAP authentication. I will not include all lines as it would be too long.

Login_action.cfm

<cfinclude template="ldap.cfm">

<!--- LDAP login code goes here; checks if credential's supplied are valid and sets variable #logged_in# ... --->

<cftry>

    <cfif isdefined("logged_in") and logged_in>

        <!--- Create Session User Structure and Default Profile --->

        <cfset session.user=StructNew()>

        <cfset session.user.username=form.username>

        <cfset session.user.firstname="">

        <cfset session.user.surname="">

        <cfset session.user.email="">

        <!--- Pull in additional Details --->

        <cfquery name="qDetails" datasource="userdetails">

        SELECT firstname,surname,email FROM Profiles

        WHERE username = '#session.user.username#'

        </cfquery>

        <cfif isdefined("qDetails") and qDetails.recordcount>

            <cfoutput query="qDetails">

            <cfset session.user.firstname ="#qDetails.firstname#">

            <cfset session.user.surname="#qDetails.surname#">

            <cfset session.user.email="#qDetails.email#">

            </cfouput>

        </cfif>

    <cfelse>

        <!--- Throw Error back to login page --->

        <cfthrow message="1" type="Custom_Security" ErrorCode="Fail">

    </cfif>

    <cfcatch type="any">

        <!--- Throw Error back to login page --->

        <cfthrow message="2" type="Custom_Security" ErrorCode="Fail">

    </cfcatch>

</cftry>

<!--- Redirect User to login page after Successful Login --->

<cflocation url="index.cfm">

Index.cfm

<!--- At Top of Index session.user.email always exists, in the event it is undefined it will be set to "" --->

<cfparam name="session.user.email" default="">

<!--- Lots of lines of code above here build HTML page etc <cfoutput> references to #session.user.email# and other session vars --->

<!--- Example pull some more information this line may or may not error... --->

<cfquery name="qUserInformation" datasource="userdetails">

SELECT * FROM Information

WHERE user_email = '#session.user.email#'

ORDER BY SomeOtherColumn

</CFQUERY>

<!--- Example point where a session error can be generated less than 2 lines above it has just finished getting information using session.user.email --->

<cfquery name="qUserGroups" datasource="userdetails">

SELECT * FROM Groups

WHERE user_email = '#session.user.email#'

ORDER BY SomeColumn

</CFQUERY>

<!--- Some other common examples require cfscript - and throw errors; above lines may not ... --->

<cfscript>

oHomePage=CreateObject("component","site_components.homepage");

qNews=oHomePage.getNews("#session.user.category#");

</cfscript>

<!--- More lines to do with HTML Footer etc <cfoutput> more stuff --->

in the wwwroot the Application.cfm handles all errors generated during rendering of the page, which includes generating a cfdump of the session scope.

When I get the Error Log I can see in the cfcatch cfdump the error... Element USER.EMAIL UnDefined in SESSION.

struct

Browser

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36

DateTime

{ts '2014-09-28 11:38:48'}

Diagnostics

Element USER.EMAIL is undefined in SESSION. <br>The error occurred on line 15.

However when I check the SESSION scope which is also dumped ... It is defined!

struct

user

struct

COLOR

cccccc

EMAIL

myemail@example.com

FIRSTNAME

Example

SURNAME

Surname

EXAMPLE1

1

TEMPLATE

template14

TEMPLATE_DETAILS

0

What is causing this strange and irratic behaviour? The problem is so far I cannot replicate the issue. It is intermittant. The issue only started after upgrading to ColdFusion 11. This code has worked flawlessly for years on CF8.

BKBK
Community Expert
Community Expert
September 29, 2014

Clearly, if logged_in is undefined in the context of  login_action.cfm, then session.user.email will be undefined. The 2 relevant questions are: where is the cfcatch cfdump you talk about and where do you dump the session?

How does does your cfapplication tag look like? You have to enable session management properly. Otherwise Coldfusion may create a new session at every request.