Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Persistent Cookies in cfscript

DZone's Guide to

Persistent Cookies in cfscript

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

I just saw a question pop up on twitter about how to set persistent cookies in cfscript. As cfcookie hasn't been ported to cfscript, then you have to set the response headers manually. Here's an example of setting a cookie which expires after a year.
getPageContext().getResponse().addHeader("Set-Cookie", "somevarA=foo; expires=#DateFormat( DateAdd( "yyyy", 1, Now() ), "ddd, dd-mmm-yyyy" )# #TimeFormat( Now(), "HH:MM:SS" )# GMT");

Woah - that looks nasty! A shorter version is to specify the number of seconds you want the cookie to expire after, so you could do this instead:

getPageContext().getResponse().addHeader("Set-Cookie", "somevarB=bar; Max-Age=31622400" );

Not quite as readable, but shorter. Both examples do the same thing.

The advantage of doing it this way is that you can add unsupported attributes (for example HTTPOnly was only added in CF9). You can find more information about cookies here:

http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}