Search code examples
coldfusioncoldfusion-2016cfcookie

ColdFusion check when cookie expires


Is it possible to check when a cookie is due to expire? I have tried the following:

First I set three cookies:

<cfcookie name="test1" value="" expires="10" />
<cfcookie name="test2" value="" expires="never" />
<cfcookie name="test3" value="" expires="now" />

Then on another page I check the cookie data:

<cfset cookies = getPageContext().getRequest().getCookies()>
<Cfoutput>
    <cfloop index="c" array="#cookies#">#c.getName()#:#c.getMaxAge()#<br> 
    </cfloop>
</Cfoutput>

However MaxAge returns -1 for all cookies instead of the actual expiration date. How can I get the actual expiration date?


Solution

  • Attempting to answer this question (only because this is a ColdFusion question and I thought it would be rude to mark this a duplicate of a Java question without discussing it.) without completely plagiarizing this Java answer.

    getPageContext().getRequest().getCookies() this basically gets you the cookies that were sent to the server by the browser. The browser only sends back the name and value of the cookie. So once the cookie is set, there is no way for the server to know when that Cookie is going to expire. You might need to save the cookie expiry on the server side when you are setting it.