質問

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

役に立ちましたか?

解決

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

他のヒント

[UPDATE: this answer is wrong. I've only left it here as I've done some interesting investigation into the situation, which might be worth people reading]

What @Travis said was the problem - that "when the form structure no longer exists, neither does your postdata structure" - is absolutely not true. The reference to the form scope might have gone, but as long as there's any reference to the data (like request.session.postData) then the underlying object will not be removed.

The problem is likely to be that when you do your redirect, OnRequestEnd.cfm doesn't run for that request, so the variables in request.session never get copied into the session scope. It all depends on how you're doing the redirect. I'd've assumed it'd have been with a <cflocation>, except you say you're using CFMX7 which can't do a 301 (which you're say you're doing): it can only do a 302. Can you pls clarify how you're doing this redirect? Cheers.

Digression: you're adding quite a lot of overhead plus a bottleneck at either end of the request in continuing with all this legacy session-locking shenanigans. You really ought to put aside some time to pull it all out. This locking session-scope-locking carry-on hasn't been necessary since CF5. Also if your session scope contains structs, you're not mitigating the problem anyhow. This is, obviously, an aside; and nothing to do with your actual question. I just figured it was worth mentioning.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top