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 developer advocate for HERE Technologies. He focuses on JavaScript, serverless 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