Copy link to clipboard
Copied
I have been having problems setting and reading a cookie. I broke it down to a really simple snippet of code:
<cfcookie name="cookie.test"
value="hello"
expires="Never">
<CFIF structkeyexists(cookie,"test")>
Cookie exists
<cfelse>
Cookie does not exist
</cfif>
When I load this page in Chrome, I can use the Dev tools in the browser to see that the cookie was set.
Coldfusion always says the cookie doen't exist (initially, I want to read the value, but I can do that because it claims it does not exist).
So, it seems the server is happy to write the cookie, but not to read it. Is there a server setting somewhere that might need to be altered?
Lots more could be said, but at minimum, what if you try <cfcookie name="test"... instead of "cookie.test"?
@pkonshak , your code is good. But you tested for the wrong key.
This should work:
<cfcookie name="cookie.test"
value="hello"
expires="Never">
<CFIF structkeyexists(cookie,"cookie.test")>
Cookie exists
<cfelse>
Cookie does not exist
</cfif>
A cookie may have a dot in its name. 🙂 In this case,
Copy link to clipboard
Copied
Lots more could be said, but at minimum, what if you try <cfcookie name="test"... instead of "cookie.test"?
Copy link to clipboard
Copied
Wow, something so simple and I completely missed it. That took care of it. Thanks Charlie, I really appreciate it!
Copy link to clipboard
Copied
Glad to help. I wanted to make sure that alone would solve your problem. 🙂
That said, a few more points seem worth your considering.
First note that technically you COULD use the "cookie." prefix/scope--with OTHER than cfcookie. It refers to cookies set in or passed to a cf page (more in a moment).
More specifically, neither approach to setting a cookie will actually "set" the cookie in the browser until the page runs. That's what causes the cookie to be "sent" to and"set in" the browser.
So in the cfif above, which follows setting the cookie, you're technically not testing anything about the cookie having been set in and returned from the browser. Instead it's in effect testing what WOULD be set, per that code above it setting the cookie. I doubt that's what you intended.
These may seem pedantic points to some, or obvious ones to others, but having helped people with cookie challenges for 25 years, they're points I find many fail to fully understand--and they're indeed very important to understand when working with troubleshooting cookie problems.
Hope that's helpful.
Copy link to clipboard
Copied
@pkonshak , your code is good. But you tested for the wrong key.
This should work:
<cfcookie name="cookie.test"
value="hello"
expires="Never">
<CFIF structkeyexists(cookie,"cookie.test")>
Cookie exists
<cfelse>
Cookie does not exist
</cfif>
A cookie may have a dot in its name. 🙂 In this case,
Copy link to clipboard
Copied
@pkonshak , for a bit of fun:
Set-Cookie: COOKIE.TEST=hello;