Skip to main content
glamorous_Wonder6C1C
Inspiring
January 17, 2013
Question

Session variable undefined when it should be.

  • January 17, 2013
  • 2 replies
  • 3615 views

Hi All,

I am getting a weired session issue. It is showing me session variable is undefined when it should be defined.

Environment details:

Web Server:IIS7

OS:Windows Server 2008 R2 Standard, Service Pack 1 64 bit

Application Server:CF9

Problem Description: It is a page for collection customer review. I have used <cfparam> to declare all session variable that I am going to use

and use StructKeyExists function for checking variable exsistant. The problem is that it is throwing variable undefined error just after checking

for the exsistant of that variable.

Here is the code snippet. :-

<cfparam name="session.productReview" default="#StructNew()#">

<cfparam name="session.productReview.productReviewCustomerCity" default="">

<cfparam name="session.productReview.productReviewCustomerState" default="">

<cfparam name="session.productReview.productReviewCustomerEmail" default="">

<cfparam name="session.productReview.productReviewCustomerFirstName" default="">

<cfparam name="session.productReview.productReviewCustomerLastName" default="">

<cfparam name="session.productReview.productReviewCustomerRating" default="">

<cfparam name="session.productReview.productReviewCustomerReview" default="">

          <!---get states (US)--->

    <cfset getStates = application.toolsObj.viewStates()>

   

    <!---get the provinces (Canada)--->

    <cfset getProvinces = application.toolsObj.viewProvinces()>

   

    <!---get the list of contries--->

    <cfset getCountries =  application.toolsObj.viewCountries()>

   

    <cfset session.productReviewHumanFlag = 0>

    <cfparam name="session.productReviewHumanFlaguuid" default="#CreateUUID()#">

    <script>

         productReviewUnique = "<cfoutput>#session.productReviewHumanFlaguuid#</cfoutput>";

    </script>

   

    <cfoutput>

   

        <form name="theProductreviewForm" id="theProductreviewForm" format="html" action="/components/formProcessing.cfc" onsubmit="return checkForm(this)"

         enctype="multipart/form-data" method="post">

            <input type="hidden" name="method" value="someMethod"/>

            <table width="100%" border="0" cellspacing="0" cellpadding="2">

               

                <tr>

                    <td valign="top" class="label error-label" nowrap="nowrap">State</td>

                    <td class="side-padding-10">

                        <select name="productReviewCustomerState" id="productReviewCustomerState" class="field-75" req="open"

                        message="State is required">

                            <option value="" selected="selected"></option>

                            <cfif isDefined("getStates") AND getStates.recordCount GT 0>

                                <optgroup label="States">

                                    <cfloop query="getStates">

                                        <cfif StructKeyExists(session, "createAccount")

                                        AND StructKeyExists(session.createAccount, "productReviewCustomerState")

                                        AND session.createAccount.productReviewCustomerState EQ getStates.State>

                                            <cfset page.selectedState = ' selected="selected"'>

                                        <cfelseif StructKeyExists(session, "productReview")

                                        AND StructKeyExists(session.productReview, "productReviewCustomerState")

                                        AND session.productReview.productReviewCustomerState EQ getStates.State >

                                            <cfset page.selectedState = ' selected="selected"'>

                                        <cfelse>

                                            <cfset page.selectedState = ''>

                                        </cfif>

                                        <option value="#getStates.State#"#page.selectedState#>#getStates.State#</option>

                                    </cfloop>

                                </optgroup>

                            </cfif>

                            <cfif isDefined("getProvinces") AND getProvinces.recordCount GT 0>

                                <optgroup label="Provinces">

                                <cfloop query="getProvinces">

                                    <cfif StructKeyExists(session, "createAccount")

                                    AND StructKeyExists(session.createAccount, "productReviewCustomerState")

                                    AND session.createAccount.productReviewCustomerState EQ getProvinces.Province>

                                        <cfset selectedProvince = ' selected="selected"'>

                                    <!--- I have got error with this line below --->

                                    <cfelseif StructKeyExists(session, "productReview")

                                    AND StructKeyExists(session.productReview, "productReviewCustomerState")

                                    AND session.productReview.productReviewCustomerState EQ getProvinces.Province >

                                        <cfset selectedProvince = ' selected="selected"'>

                                    <cfelse>

                                        <cfset selectedProvince = ''>

                                    </cfif>

                                    <option value="#getProvinces.Province#"#selectedProvince#>#getProvinces.Province#</option>

                                </cfloop>

                                </optgroup>

                            </cfif>

                        </select>

                    </td>

                </tr>

               

                <tr>

                    <td align="right" colspan="2">

                                                            <input type="image" name="imageField" id="imageField" src="/assets/images/button-save.png" />

                                                  </td>

                </tr>

            </table>

        </form>

    </cfoutput>

    </cfif>

   

   

    <cfif StructKeyExists(session, "productReview")>

   

        <!---reset all session variable--->

        <cflock scope="session" type="exclusive" timeout="60" throwontimeout="false">

                  <!---product review structure--->

            <cfset session.productReview = StructNew()>

            <cfset session.productReview.productReviewCustomerCity = "">

            <cfset session.productReview.productReviewCustomerState = "">

            <cfset session.productReview.productReviewCustomerEmail = "">

            <cfset session.productReview.productReviewCustomerFirstName = "">

            <cfset session.productReview.productReviewCustomerLastName = "">

            <cfset session.productReview.productReviewCustomerRating = "">

            <cfset session.productReview.productReviewCustomerReview = "">

            <cfset session.productReview.errorMessage = "">

            <cfset session.productReview.formSubmitted = 0>

        </cflock>

    </cfif>

As you can see that I am using <cfparam> for "session.productReview" but still it showing me variable undefined.

ERROR Details:

  • User Browser: Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; DROID RAZR 4G Build/6.7.2-180_DHD-16_M4-31) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
  • Date and Time the Error Occurred: 01/16/2013 at 09:31:06
  • Error type: Element PRODUCTREVIEW.PRODUCTREVIEWCUSTOMERSTATE is undefined in SESSION.
  • Error:
  • Message Content:
  • coldfusion.runtime.UndefinedElementException: Element PRODUCTREVIEW.PRODUCTREVIEWCUSTOMERSTATE is undefined in SESSION.

    EVENTNAME[empty string]
    EXCEPTION
    struct
    Detail[empty string]
    ErrNumber0
    MessageElement PRODUCTREVIEW.PRODUCTREVIEWCUSTOMERSTATE is undefined in SESSION.
    ResolvednameSESSION



    Here is the dump of session variable after the error in application.cfc "Onerror" Method.

    productquestion
    session - struct
    ERRORMESSAGE[empty string]
    PRODUCTQUESTIONCUSTOMEREMAIL[empty string]
    PRODUCTQUESTIONCUSTOMERNAME[empty string]
    PRODUCTQUESTIONCUSTOMERQUESTION[empty string]
    QUESTIONASKED0
    productreview
    session - struct
    ERRORMESSAGE[empty string]
    FORMSUBMITTED0
    PRODUCTREVIEWCUSTOMERCITY[empty string]
    PRODUCTREVIEWCUSTOMEREMAIL[empty string]
    PRODUCTREVIEWCUSTOMERFIRSTNAME[empty string]
    PRODUCTREVIEWCUSTOMERLASTNAME[empty string]
    PRODUCTREVIEWCUSTOMERRATING[empty string]
    PRODUCTREVIEWCUSTOMERREVIEW[empty string]
    PRODUCTREVIEWCUSTOMERSTATE[empty string]

    Any help will be appriciated, also please inform me if anybody need some more information regarding this issue.

    It's really pissing me off.

Thanks

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
January 19, 2013

For a start, delete the stray '</cfif>' located just after the cfoutput closing tag.

saurav Pandit wrote:

I am getting a weired session issue. It is showing me session variable is undefined when it should be defined.

Chances are, the session variable is simply undefined. You have yet to give us perhaps the most relevant information, that is, where this piece of code is located. 

<cfif StructKeyExists(session, "productReview")>
   
        <!---reset all session variable--->
        <cflock scope="session" type="exclusive" timeout="60" throwontimeout="false">
                  <!---product review structure--->
            <cfset session.productReview = StructNew()>
            <cfset session.productReview.productReviewCustomerCity = "">
            <cfset session.productReview.productReviewCustomerState = "">
            <cfset session.productReview.productReviewCustomerEmail = "">
            <cfset session.productReview.productReviewCustomerFirstName = "">
            <cfset session.productReview.productReviewCustomerLastName = "">
            <cfset session.productReview.productReviewCustomerRating = "">
            <cfset session.productReview.productReviewCustomerReview = "">
            <cfset session.productReview.errorMessage = "">
            <cfset session.productReview.formSubmitted = 0>
        </cflock>
    </cfif>

Most of this is repetition, hence unnecessary. You have already initialized the productReview variables, using cfparam. Therefore, what you require here is simply:

<cfif StructKeyExists(session, "productReview")>
        <cfset session.productReview.errorMessage = "">
        <cfset session.productReview.formSubmitted = 0>
</cfif>

This is initialization code, so the lock is unnecessary.

glamorous_Wonder6C1C
Inspiring
January 25, 2013

Chances are, the session variable is simply undefined. You have yet to give us perhaps the most relevant information, that is, where this piece of code is located.

I have a page showing product information. I am includeing this page(code you are seeing) is in different page. I use <cfinclude> tag to include the cod in page where I am showing product information.

Page1: Pr_detail.cfm

Page2:Pr_review.cfm

So I am include "Pr_review.cfm" in "Pr_detail.cfm" using cfinclude.

"Most of this is repetition, hence unnecessary"

The repetition is for initializing the session variable. It is required because the form data is being stored in "session.productReview" after submitting form. So after each submission I am re-initializing the session variable.

Thanks a lot for replying and excuse me for late reply.

Please inform me if you need more information or simply my reply is not clear.

Happy weekend..:)

WolfShade
Legend
January 17, 2013

What is the set timeout for your session scope in the CF Admin?

I was just informed, yesterday, that our session is set to timeout after 8 hours.  We are having intermittent session variable issues.  Apparently, something is emptying the session scope without destroying the variables, so when we check with StructKeyExists, the variable exists, but it's either empty, null, or corrupted.

^_^

glamorous_Wonder6C1C
Inspiring
January 17, 2013

We are having intermittent session variable issues

--My issue is also intermittent . We all have tried to reproduced it many way but unable to solve the mystry.

     

What is the set timeout for your session scope in the CF Admin?

--In my application we have set it to 1 hour. I will inform you CF Admin setting later.

Do have any guess why it is hapening?

Thanks for your reply and inform me if you need any other information.

Saurav

WolfShade
Legend
January 17, 2013

Unfortunately, we have not yet been able to determine what is causing this session issue.

Best of luck in figuring your situation out.  Maybe if you set the timeout to 20 minutes (default for CF), that might fix it.  Our higher-ups want it set to 8 hours, so we can't arbitrarily adjust to a lower timeout.

^_^