Skip to main content
Known Participant
December 15, 2022
Answered

Trouble reading cookies

  • December 15, 2022
  • 2 replies
  • 543 views

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?

    This topic has been closed for replies.
    Correct answer BKBK

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

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    December 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# 
    BKBK
    Community Expert
    Community Expert
    December 16, 2022

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

     

    Charlie Arehart
    Community Expert
    Community Expert
    December 15, 2022

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

    /Charlie (troubleshooter, carehart. org)
    pkonshakAuthor
    Known Participant
    December 15, 2022

    Wow, something so simple and I completely missed it.  That took care of it.  Thanks Charlie, I really appreciate it!

    Charlie Arehart
    Community Expert
    Community Expert
    December 15, 2022

    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)