• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Trouble reading cookies

New Here ,
Dec 15, 2022 Dec 15, 2022

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?

Views

214

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 15, 2022 Dec 15, 2022

Lots more could be said, but at minimum, what if you try <cfcookie name="test"... instead of "cookie.test"?

Votes

Translate

Translate
Community Expert , Dec 16, 2022 Dec 16, 2022

@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, 

  • Name of cookie is "cookie.test"
  • Value of cookie is #cookie.cookie.test# 

Votes

Translate

Translate
Community Expert ,
Dec 15, 2022 Dec 15, 2022

Copy link to clipboard

Copied

Lots more could be said, but at minimum, what if you try <cfcookie name="test"... instead of "cookie.test"?


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 15, 2022 Dec 15, 2022

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2022 Dec 15, 2022

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). 

  • You can even SET a cookie that way, as in <cfset cookie.test="hello">. You seemed to be conflating the two approaches. But cfcookie gives you far more control of other characteristics of the cookie to be set. 
  • But either way you may set a cookie in a cf page, note that CF will put that in a cookie scope that lives in memory on that page until it completes, and THAT is what your code it testing, which was not what I think you expected. 

 

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. 

  • When the page setting the cookie runs, THAT'S when the browser calling that page sets it, once that page is finished.
  • Then if a page in the same site is requested by the browser, the browser SENDS to cf whatever cookies were set (in that browser, for that site, and that had not expired). 
  • So if you took OUT the code above setting the cookie, then THAT would be testing if a cookie of that name may have been "set in and sent back from" the browser. 
  • FWIW, note also that CF's cgi.http_cookie variable shows what cookies (if any) WERE passed into a cf page 

 

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. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 16, 2022 Dec 16, 2022

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, 

  • Name of cookie is "cookie.test"
  • Value of cookie is #cookie.cookie.test# 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 16, 2022 Dec 16, 2022

Copy link to clipboard

Copied

LATEST

@pkonshak , for a bit of fun:

  1.  Load the CFM page of your original ("unsuccessful") test in a browser.
  2.  Open Developer Tools (by pressing F12 key).
  3.  Press F5 to refresh the page.
  4.  In the Developer Tools Window, click on the URL of the CFM page, then on the Headers tab. You will see the following among the Response Headers:
    Set-Cookie: COOKIE.TEST=hello;

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation