coldfusionhttp-status-code-301coldfusion-7application.cfm

Application.cfm missing Session variables after 301 redirect


Running ColdFusion 7.

The Application.cfm loops all session variables into request.session

The OnRequestEnd.cfm loops all request.session values back into session

It does this so it only needs to lock the scope once when writing the variables in a single transaction. (I believe this isn't so much an issue anymore? Yet I can't exactly rip it out).

I have a 'redirect.cfm' page which either provides a 301 redirect to an SEO URL or delivers the content. Some forms post to the old URL, so need a 301 redirect which causes the loss of POST data. This is how I intended to handle it.

<!--- if form scope exists (posted data) copy it to the request.session scope ---> 
<cfif structKeyExists(form,'fieldNames')>
    <cfset request.session.postData = structCopy(form)>
</cfif>

Then it moves on to a 301 redirect, and when it comes back to redirect.cfm to deliver the content it runs this code

<!--- if request.session.postData exists (posted data) copy it to the form scope --->
<cfif structKeyExists(request.session,'postData')>
    <cfset form = structCopy(request.session.postData)>
    <cfset StructDelete(request.session,'postData')>
</cfif>

This works fine if a 301 redirect is not needed from post of data.

With a 301 redirect, I have confirmed the Application.cfm, OnRequestEnd.cfm both run twice (once for the initial 301 and once for the content delivery).

By the end of the first OnRequestEnd.cfm call session.postdata is populated correctly with the form data.

After the 301 redirect and it hits Application.cfm again the session.postdata returns 'struct[empty]'

Any help? Thanks


Solution

  • structCopy() creates a shallow copy of the structure, meaning that nested structures are by reference only which is why your simple values persisted but your nested structures did not. Once your form structure no longer contained data your postData structure began referencing an empty structure so your reference is also empty.

    To do a "Deep Copy" of your structures use duplicate()

    See also other structure functions

    CF 9 documentation for deleting structures