Ask a Jedi: Creating lowercased cookies in ColdFusion

Chris asks:

When creating a cookie with cfcookie, the cookie is created in uppercase characters. Is there a way to make it lowercase?

What he means is, if you do this:

<cfcookie name="x" value="Foo">

And you examine your cookies, you will see that ColdFusion created the cookie and named it X. This may not seem like a big deal, but it gets important if you want to read the cookie in JavaScript.

I tried to get fancy and set a cookie like so:

<cfset cookie["y"] = 2>

But that made no difference. I did a quick Google and discovered a tech note from - wait for it - 2004:

The cfcookie tag is unable to create or delete mixed case or lowercase cookies

The solution they provide though still works today. You can use the cfheader tag to make the cookie yourself:

<cfheader name="Set-Cookie" value="mycookie=z;expires=#DateFormat(CreateDate(2009,12,31), 'ddd, dd-mmm-yyyy')# #TimeFormat(CreateTime(00,00,00), 'HH:mm:ss')# GMT">

Things get weird though if you use both forms:

<cfheader name="Set-Cookie" value="mycookie=z;expires=#DateFormat(CreateDate(2009,12,31), 'ddd, dd-mmm-yyyy')# #TimeFormat(CreateTime(00,00,00), 'HH:mm:ss')# GMT"> <cfset cookie.mycookie = 'm'>

I should have two cookies, one named mycookie, value z and another named MYCOOKIE, value is m. But when you view the cookie scope from ColdFusion, you see 2 keys with the exact same name (MYCOOKIE), both with a value of m.

So watch out if you are using ColdFusion to manipulate cookies that may also be manipulated via JavaScript.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate looking for his next gig. 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.

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

Comments