Skip to main content
Inspiring
November 23, 2011
Question

cfcookie not working (CF9)

  • November 23, 2011
  • 2 replies
  • 7919 views

I use this line of CF code to set new cookies :

<cfcookie name="PLARTS_MAIL_USERNAME" value="#trim(form.username)#" expires="410">

And i cannot see my cookie in FireFox , or readding it with Javascript,

or using CF read with : <cfparam name="cookie.PLARTS_MAIL_USERNAME" type="string" default="" />

It always return "blanc" , not existing.

(I can see other cookies from other Sites).

Any idea ? any help thanks.

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
November 24, 2011

plarts wrote:

I use this line of CF code to set new cookies :

<cfcookie name="PLARTS_MAIL_USERNAME" value="#trim(form.username)#" expires="410">

And i cannot see my cookie in FireFox , or readding it with Javascript,

or using CF read with : <cfparam name="cookie.PLARTS_MAIL_USERNAME" type="string" default="" />

It always return "blanc" , not existing.

(I can see other cookies from other Sites).

Two points:

1) If your application file is Application.cfm, then give the attribute setClientCookies (in the cfapplication tag) the value "yes". If you're using Application.cfc instead, then give this.setClientCookies the value "yes".

2) I created the following 2 CFM pages, and ran testPage.cfm. It works!

cookiePage.cfm

<cfcookie name="bkbk" value="testing testing 1, 2, 3" expires="10">

testPage.cfm

<cfinclude template="cookiePage.cfm">

<cfdump var="#cookie#">

plartsAuthor
Inspiring
November 24, 2011

I fully agree with what you did.

outside my application, all is working well. (small testing)

Inside my application, not.

But it is simple, there is a form for LOGIN.

When submitted, I create the cookies, works well, I can see my cookies.

If I refresh the page, (so resubmit, which is the same process) the cookies are lost.

I mean, I cannot see them anymore.

If I go to another page after the Login, same, the cookies are lost.

And there is nowhere expires='now' for these cookies.

OK, I give up. I keep it like this, no cookies for the LOGIN. (I only use the session variable to remember)

In same application, I use another cookie elsewhere, all is working fine.

Inspiring
November 28, 2011

When I do the same thing with simple pages, it works and cookies are kept.

one page set up cookies, then go to another page, and I still have the cookies.

In same directory, so with same application.cfm.

So, I take out piece and piece of codes in the real application and see when the cookies are lost.

Then I found it :  a CFFLUSH

I have a <cfflush interval=10>

with it, I loose the cookies, if I take it out

I keep the cookies.

Why, I do not know.

Any suggestion ?


Documentation for <cfflush>:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_f_12.html

Using cfflush prevents any cokies from being set. Makes sense since you are flushing content (including header content) to the browser in a way that probably interferres with normal cookie setting.

Inspiring
November 23, 2011

What happens if you dump out your COOKIE scope immediately after setting it?

<cfcookie name="PLARTS_MAIL_USERNAME" value="#trim(form.username)#" expires="410">
<cfdump var="#COOKIE#">

Also, you're not doing a <cflocation> on the same page that you are setting your cookie are you?

plartsAuthor
Inspiring
November 23, 2011

Thanks for this view with Dump.

I did a small program to set cookies and read them, it does work.

But inside my application , it does not work, no cookies are created.

So I think I have to look deeply in the apllication, why it does does not work.

Inspiring
November 23, 2011

There are typically 2 common reasons why cookies do not get set in CF applications:

  1. The developer is trying to set a cookie and then performs a cflocation redirect.  In this case, the redirect command is sent to the browser and the browser never receives the cookie set command
  2. The user's browser rejects the cookie due to security/privacy issues.  Typically you will see this if the site is missing a P3P privacy policy - many modern browsers reject cookies outright from sites that don't declare their intentions on how they want to use the data.