ColdFusion Zeus POTW: Cookie via Script

This post is more than 2 years old.

In my last Zeus preview, I discussed what I thought was a minor, but nice, language enhancement. Turns out the "minor" feature got quite a few replies from my readers. So in that vein, I'm posting another "minor but good" improvement this week. In current versions of ColdFusion, you have two ways to set cookies:

<cfcookie name="chocolate" expires="10" value="yum"> <cfset cookie.saints = "StopBeingOverConfident">

The tag version of cfcookie allows you to set a cookie name and value as well as a set of attributes that control how long the cookie persists and how it is accessed. The script version (or the 'simple cfset' version) only allows you to set a name and value. ColdFusion sets this cookies as a session cookie which means it will go away as soon as you close your browser window.

ColdFusion Zeus fixes this in a rather simple, and elegant way. You can still set cookies exactly the same way you do now. But if you want to specify specific attributes of the cookie, you can use a structure instead:

<cfscript> cookie.age = {value="38", expires="never"}; </cfscript>

You can specify any valid value for the cfcookie tag as a name/value pair in the structure.

So nothing earth shattering, but it's one more thing you can now do in cfscript versus tags.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Todd Rafferty posted on 10/31/2011 at 6:17 PM

And, in case it isn't obvious to people that hate using the cfscript tag for one line of code, you can set via: <cfset cookie.age = {value="38", expires="never"}>

Comment 2 by Raymond Camden posted on 10/31/2011 at 6:22 PM

But why would you do that? That's more typing than just the tag. I think it's pretty implied that the blocks above would be in templates with more code.

Comment 3 by Todd Rafferty posted on 10/31/2011 at 6:49 PM

You'd be surprised at the amount of:
<cfscript>
one line of code;
</cfscript>
I see at work and in open source projects. I bitch about this all the time. :)

Comment 4 by Frຝéric Fortier posted on 10/31/2011 at 8:38 PM

I agree with Todd on this one.. when I only have to set one variable, <cfset> it is for me.

I always depends on the context I guess...

Comment 5 by Raymond Camden posted on 10/31/2011 at 8:42 PM

I definitely agree - but again - this is not really the point here. Right guys? :)

Comment 6 by Gary posted on 10/31/2011 at 9:11 PM

i use <cfscript>one line of code;</cfscript> all the time and i don't see a problem with that. It's just a personal preference and i don't understand why people pick about it. besides when you need to add more lines, it makes that easier rather than changing from cfset to cfscript.

Comment 7 by John Sieber posted on 10/31/2011 at 10:18 PM

I agree with Todd that it makes sense if that is the only line in your script tags, or if you are not using script syntax in your code yet.

Comment 8 by Raymond Camden posted on 10/31/2011 at 10:22 PM

Heh, so, is _no_ one going to comment on the feature? You guys drive me crazy sometimes. But then again - I appreciate the surprising way folks react sometimes! :)

Comment 9 by Peter Boughton posted on 10/31/2011 at 11:36 PM

Age expires never - you've invented immortality!? :D

Comment 10 by Gary F posted on 11/1/2011 at 2:10 AM

Hmm. I'll probably stick with cfcookie because of attribute hinting in Dreamweaver and it's daft to switch over to cfscript just for 1 or 2 lines if it's not necessary.

Would the next release of Dreamweaver or even CFBuilder support attribute hinting inside parentheses, I wonder?

Yay for your blog entry, Ray! :-) From us crazies.

Comment 11 by Raymond Camden posted on 11/1/2011 at 5:19 AM

CFB kinda does it now for ormsettings within App.cfc.

Comment 12 by Peter Tilbrook posted on 11/1/2011 at 3:01 PM

Actually the more I use CFSCRIPT the more I like it.

Agreed for one off or code "ready" ness for only one or two settings but for lengthy assignments it is more elegant and faster.

Comment 13 by Jeremy Battle posted on 11/1/2011 at 6:46 PM

I am actually happy about the addition of this to cfscript. I write almost exclusively in script these days and any time I can remove a section of code from my "nonScriptFunctions.cfc" is a good day for me.

Comment 14 by Michael posted on 11/9/2011 at 2:12 PM

For me it looks a bit weird.
The struct notation is used here not only to set values but for functionality behind. I'd definitly prefer a cookie() function.