Session variables not saved after cflocation or relocate()

I am having a problem with session variables. The values I assign to
them are not being saved when I use any relocate method, either from a
handler or from a template.

I have a handler that redirects to an external site (Twitter), but I
have also tried this when redirecting to internal methods as well and
get the same results.

When I set session variables inside a handler and then call a
cflocation after, the session variables are not saved. It doesn't
matter if I call the cflocation from the handler itself, or from the
layout/view template the handler sets.

I get this behavior when I use cflocation, setNextEvent or the
relocate() method.

Example:

  <cffunction name="example" returntype="any" output="false">
    <cfargument name="event" required="true">
    <cfset var rc = event.getCollection()>

    <cfscript>

    if (isDefined("rc.test")) {
    session.test = rc.test;
    }

    event.setLayout("Layout.Example");

    </cfscript>

  </cffunction>

If I call index.cfm?event=my.example&test=mike and then do: <cfdump
var="#session#"> on the Layout.Example.cfm file, I see the correct
value for session.test, the handler sets session.test to "mike" and I
see mike on the page. However, if I do:

  <cffunction name="example" returntype="any" output="false">
    <cfargument name="event" required="true">
    <cfset var rc = event.getCollection()>

    <cfscript>

    if (isDefined("rc.test")) {
    session.test = rc.test;
    }

    </cfscript>

        <cflocation url="http://www.twitter.com">

  </cffunction>

I am relocated to Twitter, but if I check back with my page that has
the session dump again, session.test hasn't changed.

The weird part is, if I dump the session to a file just before the
cflocation, the values are correct, but after the cflocation, the
values are not saved and are different from the ones saved in the
file. Something about using a relocation is causing the session
variables not to save. They have the correct values temporarily before
the relocation, but then return to whatever value they were before the
handler was called.

The oddest part is that if the variable has not been set yet (if
session.test doesn't exist), the value will be set the first time I
call the handler and redicrect, but later requests won't save it.

So if I call index.cfm?event=my.example&test=mike when session.test
doesn't exist, the handler correctly sets session.test and relocates
to Twitter. If I check my session dump page, the value is saved and
correct. However, calling index.cfm?event=my.example&test=somethingnew
AFTER session.test exists causes the session variables not to be saved
after a relocation, the value stays "mike" and doesn't change to
"somethingnew", even though if I dump the session right before the
location, the value for test IS "somethingnew" like it should be, but
back to "mike" after the relocation.

Can anyone help with what is causing this? Thank you!

Does anyone have any ideas about this? I'm using ColdBox m6

Are you sure you're still in the same session when you're brought
back? If you're looking at a different session, that would explain why
the values are missing.

- Gabriel

This is CF and not coldbox, check your timeouts, cookies, etc.
Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

I am using Railo, not ColdFusion.

Did you try the example? If I set an *existing* session variable to a
new value and then dump the session just before the setNextEvent(), I
get the new correct value, but the next request shows the old value
again. The value simply doesn't save if I use a relocate event after
setting it.

The cookies are the same, the session ID is the same (and all other
session variables are available and correct when I'm returned to the
page, including any new session variables I set that didn't exist
before the relocate). This still happens even when I don't redirect to
an external site, but only use a setNextEvent(). Remember, if the
variable doesn't exist yet, it saves fine, but it won't save existing
variables.

I set up another test page that doesn't use ColdBox, instantiated a
CFC, set a session variable in it, relocated and my session variables
set before the relocate are saved correctly.

I can confirm 100% that this is ONLY happening from within ColdBox,
not from within other ColdFusion components or templates.

The major indicator is that if the session variable doesn't exist
before the relocate, it is created and saved just fine, but if the
variable already exists, using setNextEvent() or cflocation from
within a ColdBox handler or template, the variable will not have been
saved the next time I check it.

I should add this:

        <cffunction name="example" returntype="any" output="false">
                <cfargument name="event" required="true">
                <cfset var rc = event.getCollection()>
                <cfscript>
                if (isDefined("rc.test")) {
                session.test = rc.test;
                }
                session.newVar = 'mikes new value';
                </cfscript>
        <cflocation url="http://www.twitter.com">
        </cffunction>

When I do this, session.test keeps its old value, but session.newVar
(if it didn't exist) is created and has the correct value. The dump
shows the correct value for both variables, but after the cflocation
session.test has its old value, but session.newVar has been created
with the proper value. Something is not saving existing session
variables when I cflocation from within ColdBox.

Hi, Mike:

I don't know if this will be of any help to you, however, have you tried
using the SessionStorage bean object. I am not using session storage in
my app, using instead client storage due to clustering, and I've not
experienced any issues similar to what you are reporting. Again, not
sure if this is of any value, just wanted to mention it.

Kevin

Mike there is a few more questions we need to ask.

1) Is this a single request, in another words there are no more concurrent
issues happening to duplicate this?
2) Is there any reason you are not using the cbstorage plugin?

There used to be a known issue with ColdFusion doing page relocations and
session variables, I am not sure that this is the problem in Railo and you
might need to check with the railo team on that one. Sean reads the posts in
here so he might chime in.

As Luis said this is not an issue with ColdBox but an issue with the
language itself, secondly you have no locks around your code in which you
are promoting a race condition to occur. Which is why, you should be looking
at using the cbstorage plugin instead.

Regards,
Andrew Scott
http://www.andyscott.id.au/

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of Mike Tarken
Sent: Friday, 8 October 2010 9:59 AM
To: ColdBox Platform
Subject: [coldbox:6119] Re: Session variables not saved after cflocation

or

relocate()

I should add this:

        <cffunction name="example" returntype="any" output="false">
                <cfargument name="event" required="true">
                <cfset var rc = event.getCollection()>
                <cfscript>
                if (isDefined("rc.test")) {
                session.test = rc.test;
                }
                session.newVar = 'mikes new value';
                </cfscript>
        <cflocation url="http://www.twitter.com">
        </cffunction>

When I do this, session.test keeps its old value, but session.newVar (if

it

didn't exist) is created and has the correct value. The dump shows the
correct value for both variables, but after the cflocation session.test

has its