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.

Comments

I had an issue a while back...wish I had blogged about it. The cookies from CF were coming out all uppercase, but when I would access (or create new) cookies from Javascript, they would be lower (or mixed) case. My solution, at the time, was to UPPERCASE all cookie names from ColdFusion and in Javascript. Otherwise, thisCookie was not the same at THISCOOKIE.

- Will B.
# Posted By Will B. | 4/16/08 2:13 PM
What's the reason for making cookies lowercase?
# Posted By Lola LB | 4/17/08 6:32 AM
I don't know - maybe he makes them first in JS, and wants to keep them lowercase.
# Posted By Raymond Camden | 4/17/08 6:34 AM
My understanding is that CF doesn't respect case in variable names or scopes so even though you have mycookie and MYCOOKIE in the scope when you do the cfset it probably would view them as having the same variable name. Probably would result in them both being set to m.

e.g. cookie.MYCOOKIE == cookie.mycookie == cookie.MyCookiE

CF probably assumes that all the above are the same variable and if it encounters all of them in the cookie scope it sets them all equal to m.

Just a guess...
# Posted By Daniel Sellers | 4/17/08 8:58 AM
@Daniel
Yes, that's true. But if you need to access those cookies with Javascript, the case does matter.
# Posted By Will B. | 4/17/08 9:06 AM