问题
I have a Coldfusion page with posted form values that I'm passing to a php page (via cfhttp). Example of the Coldfusion code:
<cfhttp method="Post" url="https://www.test.com/ssl/get_cookies.php" result="cookieResponse">
<cfoutput>
<cfif isdefined( "ppcid" )><cfhttpparam name="PPCID" type="formField" value="#session.ppcid#"></cfif>
<cfif isdefined( "cid" )><cfhttpparam name="CID" type="formField" value="#session.cid#"></cfif>
<cfif isdefined( "leadcomm" )><cfhttpparam name="LEADCOMM" type="formField" value="#LEADCOMM#"></cfif>
<cfif isdefined( "clk" )><cfhttpparam name="CLK" type="formField" value="#CLK#"></cfif>
<cfif isdefined( "dck" )><cfhttpparam name="DCK" type="formField" value="#DCK#"></cfif>
<cfif isdefined( "ccid" )><cfhttpparam name="CCID" type="formField" value="#CCID#"></cfif>
</cfoutput>
</cfhttp>
After I post these values to get_cookie.php, I want to set these values as cookies. Here is an example of my get_cookies.php code:
setcookie("LEADCOM", getVariable('LEADCOMM'), time()+604800, "/", ".fha.com", 0);
setcookie("CCID", getVariable('CCID'), time()+604800, "/", ".fha.com", 0);
setcookie("QTR", getVariable('QTR'), time()+604800, "/", ".fha.com", 0);
setcookie("CLK", getVariable('CLK'), time()+604800, "/", ".fha.com", 0);
setcookie("DCK", getVariable('DCK'), time()+604800, "/", ".fha.com", 0);
FYI - getVariable is function to $_REQUEST the CF variable in PHP. I check my browser and I can't see these cookies, even when I try to revisit the page. Any Suggestions?
回答1:
You set cookies in a browser. In this case the "browser" is the CFHTTP tag which does not support cookies. You can read the cookies that were set, they are returned in the cfhttp response, but they won't actually be written anywhere.
回答2:
In the case of your example above, you can think of your CFHTTP request as the "browser" that is making a request to the PHP page. The cookies you are setting in PHP are being returned to the CFHTTP result "cookieResponse". At that point they are still on the server, nothing has been returned to the initial client (the one that called the CF page to start with). If you want those to be set as cookies at this point to the end user's browser you need to then set them again using ColdFusion. Which means you'd have to parse the cookieResponse.header result, find the cookies, You can get the cookies out using cookieResponse.responseHeader["SET-COOKIE"] and set them to the end user's browser with cfheader
Like this
<cfset cookies = cookieResponse.responseHeader["set-cookie"] />
<cfloop from="1" to="#structCount(cookies)#" index="i">
<cfheader name="SET-COOKIE" value="#cookies[i]#" />
</cfloop>
来源:https://stackoverflow.com/questions/9794117/coldfusion-form-values-to-php-setcookie