Skip to main content
lovewebdev
Inspiring
July 13, 2011
Question

How to kill a coldfusion cookie??

  • July 13, 2011
  • 5 replies
  • 21491 views

I set cookies like this:

<cfcookie name="ckwhatever" value="hello">

I try to kill the cookie like this:

<cfcookie name="ckwhatever" expires="now">

This does NOT work. the cookie remains.

I've tested on mozilla and IE

I don't get it.

This topic has been closed for replies.

5 replies

New Participant
March 29, 2013

I too have had problems resetting cookies on various browsers even with CF 9.  What I've found seems to work every time is to delete the cookie, and then reset it, for example:

<!--- THIS SEQUENCE WORKS RELIABLY --->

<cfset delete_cookie=StructDelete(cookie,"member_ID")>

          <cfcookie name="member_ID" value="0" domain=".phenom.aero" expires="never">

Simply resetting or expiring the cookie does not work reliably, for example:

<!--- NONE OF THESE WORK RELIABLY --->

<cfcookie name="member_ID" value="0" domain=".phenom.aero" expires="never">

<cfset cookie.member_ID = 0>

<cfcookie name="member_ID" value="0" domain=".phenom.aero" expires="now">

Can anybody tell me why?  Am I missing something really simple here?

Known Participant
June 14, 2013

I just got this to work in an older version of Coldfusion(5).  We will be migrating soon.  Does this work in your version?

<cfif IsDefined("Cookie.YourCookieName")>

    <cfcookie name="YourCookieName" value = "#Now()#" Expires="now" domain=".YourDomainName.com">

    <cfset delete_cookie=StructDelete(cookie,"YourCookieName")>

    <cfoutput>"Cookie is Gone"</cfoutput>

    <cfabort>

<cfelse>

    <cfcookie name="YourCookieName" value = "#Now()#" Expires="never" domain=".YourDomainName.com">

    <cfoutput>"Cookie exists"</cfoutput>

    <cfabort>   

</cfif>

I put this after <CFAPPlication> with

<cfapplication

        name = "YourAPPName"

        clientmanagement="yes"

        sessionmanagement="yes"

        setclientcookies="yes"

        sessiontimeout="#CreateTimeSpan(0, 0, 30, 0)#"

        applicationtimeout="#CreateTimeSpan(0, 0, 30, 0)#" clientstorage="Registry"

>

To test it, I reload the page.  It alternates between the "Cookie is Gone" and "Cookie exists".  Let me know if that works for you.

August 17, 2011

I am experiencing issues with cookie killing as well so I read through this post.  I tried to utilize the domain attribute and that didn't make a difference.  I cannot remove my cookie from the local pc.  Frustrating.  I never used to have problems with this and it seems to work just fine in Firefox.  I am currently experiencing the problem using IE 7 (go figure).

The cookie is set like this in the Application.cfm

<cfcookie domain=www.mydomain.com name="mycookie" value="testing" expires="Never">

Here is my cookie removal code.  When a user clicks "Logout" on my site they get directed to logout.cfm which contains the code below...

<cfcookie domain=www.mydomain.com name="mycookie" value="" expires="NOW">

<cfset delete_cookie=StructDelete(cookie,"mycookie")>

<cflogout>

<cflocation url="home.cfm">

12Robots
Participating Frequently
August 17, 2011

What version of ColdFusion are you using?

Would it be possible for you to capture and post the request and response headers for the cookie set and cookie kill?

August 17, 2011

Version: cf9

I will attempt to do that but am not sure how.  Firebug?  If firebug, how specifically.

Thx

lovewebdev
Inspiring
July 14, 2011

It amazes me that coldfusion has no native way to truly just get rid of a cookie you created from the computer and from the page.

Owainnorth
Inspiring
July 14, 2011

It does, you just did it?

glamorous_Wonder6C1C
Inspiring
July 14, 2011

To test wheather we can delete the cookie variable or not i have performed the following steps.

Page Name:setcookie.cfm

Note:I am setting cookie in this page.

Code:<cfcookie name="mytestcookie" value="testvalue" expires="never">

<cfdump var="#cookie#">

<a href="killcookie.cfm">kill</a>

Output:

struct

MYTESTCOOKIEtestvalue
kill

Page Name:killcookie.cfm

Note:I am deleting and viewing cookie in this page.

Code:<!--- check wheather cookie exist or not --->

<cfdump var="#cookie#"><br>

    <!--- here i am going to delete cookie --->

    deleting cookie<br>

    <cfcookie name="mytestcookie" expires="#now()#">

<!--- dumping after deleting  --->

<cfdump var="#cookie#">

<a href="result.cfm">result</a>

output:

struct
MYTESTCOOKIEtestvalue

deleting cookie
struct
MYTESTCOOKIE[empty string]

result

Page Name:result.cfm

Note: See wheather cookie deleted or not

Code:<cfdump var="#cookie#">

Output:

struct [empty]

I think after setting expires attr in cfcookie tag the cookie variable is not getting deleted in that page.

glamorous_Wonder6C1C
Inspiring
July 14, 2011

You can use structDelete(cookie,"ckwhatever",true) to clear the cookie structure.

Owainnorth
Inspiring
July 14, 2011

True, but that won't remove the cookie from the user's browser, so the variable will come back on the next request.

Owainnorth
Inspiring
July 13, 2011

When you say it "remains", do you mean the cookie physically stays on the client PC, or you can still reference the variable in the COOKIE scope?

From the docs: "deletes cookie from client cookie.txt file (but does not delete the corresponding variable the Cookie scope of the active page)."

lovewebdev
Inspiring
July 14, 2011

Yes it was still available in scope.

But once I added the domain attribute it worked. Weird. I've never used that before, not sure why it was necessary now. It's all on the same domain.

Owainnorth
Inspiring
July 14, 2011

If you're using domain cookies then you'll need to use it here too. I.e. the domain of the cookie you're deleting will need to match up exactly with the CFCOOKIE delete statement.