Deleting a dynamically named cookie


Thanks again for looking into this question of mine.

I am looking for the correct syntex to expire a dynamically named cookie.

For example, I create the cookie below:

 <cfcookie name="dynamic#right(get.jonnumber,5)#" value="dynamic#right(get.jonnumber,5)#" expires="never"/>

Now, if I am looking to expire it, how to I correctly write out the syntex?

I have tried:

<cfcookie name="#cookie["dynamic#right(get.jonnumber,5)#"]#"  value="#cookie["dynamic#right(get.jonnumber,5)#"]#"  expires="NOW" > 

But the above syntex gets me an error....

Thanks alot !
Who is Participating?
_agx_Connect With a Mentor Commented:
Use array notation:

       <cfoutput>#cookie[ theDynamicCookieName ]#</cfoutput>
_agx_Connect With a Mentor Commented:
(EDIT) What's the error?

Nothing technically wrong with it that I can see. Worked for me with a test value. Though I think it's cleaner to do something like:

<cfset name = "dynamic"& right(get.jonnumber,5)>
<cfcookie name="#cookie[name]#"  value="#cookie[name]#"  expires="NOW" >
CFbubuAuthor Commented:
Thanks..i will try it again.

Btw, how would I get out the value of the dynamically named cookie in a <cfoutput> tag?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

CFbubuAuthor Commented:
Thanks _agx_,

I have been having a little frustration in getting this to work still.....let me try alittle longer and see what I am doing wrong and I will get back to you.

Thanks so much for your kind watch.
CFbubuAuthor Commented:

I am still not having success with anything so far...:(

I cannot even solve the simple issue of getting the value out of the dynamicallyu named cookie in the cfoutput tags.

This is my named cookie when I created it:

<cfcookie name="nopg#right(SESSION.auth.UserID,10)#" value="nopg#right(SESSION.auth.UserID,10)#" expires="never"/>

When I try to output the created cookie value, this is the syntax I tried:


This is the error I am getting:

Invalid CFML construct found on line
CFbubuAuthor Commented:
ok...I seem to have gotten the cfoutput tag to work with getting the cookie value:

 <cfoutput>#cookie[ 'nopg#right(SESSION.auth.UserID,10)#' ]#</cfoutput>

But now I cannot seem to expire the cookie.....aiyaya....

<cfcookie name="Cookie['nopg#right(SESSION.auth.UserID,10)#']"  value="#cookie["nopg#right(SESSION.auth.UserID,10)#"]#"  expires="NOW" > 

I do not get any error message on the page, but when I do a dump of the cookies on the page, I still see it.....any idea why?



I think the cookie is not expiring because somehow the syntax of the dynamic name is not being recognised so the dynamically named cookie is not found and not being expired....I am hoping you'll be able to spot what I am writing wrong in the cookie expiration tag.


DUH!.....I am so silly! of course the cookie expiration did not work because I was putting the 'cookie' word inside the expiration syntax! lol......

it works like this:

 <cfcookie name="nopg#right(SESSION.auth.UserID,10)#"  value="#cookie["nopg#right(SESSION.auth.UserID,10)#"]#"  expires="NOW" > 

Now...I will try to use your solution to resolve the earlier questions.

Sorry, I got called away.  

Yep, you figured it out. You only use the word "cookie" when you're using it as a scope.


Like with any structure, such as URL or FORM variables:

CFbubuAuthor Commented:
Thank you _agx_ .
Gah, just noticed a typo. That's what I get for testing code on one device, and typing the answer on another! This:

    <cfset name = "dynamic"& right(get.jonnumber,5)>
    <cfcookie name="#cookie[name]#"  value="#cookie[name]#"  expires="NOW" > 

should've been:

    <cfset name = "dynamic"& right(get.jonnumber,5)>
    <cfcookie name="#name#"  value="#cookie[name]#"  expires="NOW" >
CFbubuAuthor Commented:
Thanks for the clarification :)

Btw, I am not sure if this was also a typo, but to call out the value of a dynamically named cookie, when I tried to use your code syntax I got an error.

<cfoutput>#cookie[ theDynamicCookieName ]#</cfoutput>

So i copied and tried below and got an error.

  <cfoutput>#cookie[ nopg#right(SESSION.auth.UserID,10)# ]#</cfoutput>

However, when I added a ' after each [ ] I got it to so

 <cfoutput>#cookie[ 'nopg#right(SESSION.auth.UserID,10)#' ]#</cfoutput>

But I am so glad to have you always there _agx_. I think I would have died of frustration a long time ago if you did not help :P
Oh sorry, that was just meant to be an example :)  ... where "theDynamicCookieName" represents the name of any of your cookies.  

When you access any structure (COOKIE, FORM, URL, VARIABLES, etc...  ) you need to pass in a string - containing the name of the key you're trying to access:

                  someStructure[ "nameOfTheKeyHere" ]

There's a few ways different ways you can construct dynamic key names.

                 #cookie[ 'nopg#right(SESSION.auth.UserID,10)#' ]#
                 .. or
                 #cookie[ "nopg"& right(SESSION.auth.UserID,10) ]#
                 ... or
                 <cfset keyName = "nopg"& right(SESSION.auth.UserID,10) >
                 #cookie[ keyName ]#

.. it doesn't matter which you pick. The only thing that's important is that it resolves to a *string* containing the name of key - or in your case the cookie name.

> But I am so glad to have you always there _agx_.

Always glad to help :)  It's nice to have people to bounce ideas off of when you run into tricky coding problems.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.