Tricky Coldfusion Cookie Question

Posted on 2012-09-04
Last Modified: 2012-09-04

I have a quick question about the setting and use of a cookie.

Let say I have a variable 'cname' which I set value as '12345'

To create a 'dynamically' named cookie, I combine the 'cname' variable to the cookie creation tag(See code lines below)

<cfset cname = '12345'>

<cfcookie name="super#cname#" value="999" expires="1">

The question I have here is, how do I access the value of the dynamicly named cookie?

Normally to access the value of a cookie, we call it like thus:


But in the above example, how do I access the value of the 'dynamically' named cookie with a value of 999?

If I accessed the cookie using the method below, it will work, but I want to place the dynamic call to the cookie without hardcoding the '12345' into the call.


How do I somehow call the cookie value above with the dynamic name inside like below without an error?


Is this possible?

(I know that I can create the cookie and place the 'dynamic' value right inside the value ="" spot, but I want to be able to name a cookie dynamically as I want the cookie to have a unique name for keeping track purposes)

Thanks alot!
Question by:CFbubu
    LVL 51

    Accepted Solution

    Nice thing about CF is most all scopes are structures. So dynamic key names are easy. Use associative array notation  ie   scopeName["fixedPart"& dynamicPart]

    Try using

          <cfoutput>#cookie["super#cname#"]#</cfoutput> ... OR
          <cfoutput>#cookie["super"& cname]#</cfoutput>

    Author Comment

    HOORAY for _agx_!

    I have not tried out your solution I now have to rush out for about 45mins, but I will be back to try out your great teachings...yet always again!

    I am happy as your teachings will most likely they always do! Thanks for getting back to me so quick! THANKS!

    Let me return again in about 45mins to 1 hour to let you know how it went :)
    LVL 51

    Expert Comment

    No problem, take your time.  (Always nice when the answer is simpler than you think it'll be, instead of the other way around ;-)

    Author Closing Comment

    Dear _agx

    You got it again!

     I was puzzling about this all long weekend....and in the blink of an eye, you resolved it!

    Thank you so much my friend for always being so kind and generous with your knowledge.:)


    p.s. - forgot to add.....and so fast to respond to my cry for help...;)
    LVL 51

    Expert Comment

    Glad to help when I can :) I think every CF-er scratches their head about dynamic variables at first. I did too. Once you realize all scopes are structures (form, url, session, application, request, xml, etc...) a lightbulb goes on. "Ha! I can do anything dynamically!". Ok... not everything, but most things :)

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
    Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now