Cookies disappear after browser restart

262 views
Skip to first unread message

Geo

unread,
Oct 26, 2012, 9:30:07 PM10/26/12
to clj-...@googlegroups.com
Hello. Noir cookies seem to disappear after browser restart.

(defpage "/" []
  (println "cookie test: " (cookies/get :test))
  (cookies/put! :test {:value "testing" :expires 7}
  "All is well")

I have the page above defined. I open up a browser to test.

The first time I access the page the repl reads "cookie test:  nil" which is as expected.

Subsequently if I refresh the page the repl reads "cookie test:  testing" each time as expected.

However, if I quit the browser and launch it again, the console will read "cookie test:  nil" indicating the cookie was not found. I also can't locate the cookie manually. I tried this with firefox and safari and get the same behavior on both browsers.

Is this the expected behavior? I sure wasn't expecting it if it is :)

I would appreciate any help anybody can offer.

Thanks.

Geo

unread,
Oct 26, 2012, 9:35:09 PM10/26/12
to clj-...@googlegroups.com
NOTE: I missed a closing paren: (cookies/put! :test {:value "testing" :expires 7}) >> has the closing paren in the test code

I also tried setting the cookie simply (cookies/put! :test "testing") without specifying any attributes and I got the same results.

And of course I made sure my browsers allow cookies :)

Linus Ericsson

unread,
Oct 27, 2012, 7:38:26 AM10/27/12
to clj-...@googlegroups.com
Hi Geo!

If the cookie is not set with an expiration date it is removed when the browser is closed according to HTTP cookie standard (afaik). Or check your browser settings.

For full story read Wikipedia on HTTP cookies, much I didn't know about cookies there...

/Linus

Geo

unread,
Oct 27, 2012, 9:30:35 AM10/27/12
to clj-...@googlegroups.com
Thanks, Linus.

Unfortunately the problem persists (unlike the cookie :) even when I set the cookie with a expiration date.

Chris Granger

unread,
Oct 28, 2012, 4:11:19 PM10/28/12
to clj-...@googlegroups.com
I remember having to provide a specific format for the expiration date - for some reason things like expires: 7 didn't work.

Cheers,
Chris.

James Reeves

unread,
Oct 28, 2012, 4:38:12 PM10/28/12
to clj-...@googlegroups.com
HTTP cookies have two fields that control expiry. "Max-Age", which
will expire a cookie after a certain number of seconds, and "Expires",
which defines an exact date-time at which the cookie will expire.

Since Ring 1.1 you can also use JodaTime objects to define cookie
expiry dates, which is useful when combined with the clj-time library.
Any of the following should therefore work:

:expires (-> 2 hours from-now)
:expires (date-time 2013 1 1)
:max-age (-> 2 hours)
:max-age 3600

- James

Geo

unread,
Oct 29, 2012, 12:44:55 PM10/29/12
to clj-...@googlegroups.com, jre...@weavejester.com
Thanks a lot. I test all of your suggestions and found that setting :expires to any value does NOT work. However, using :max-age works as expected.

I read this at http://www.mnot.net/blog/2007/05/15/expires_max-age

"CC: max-age is just a straight integer number of seconds, while Expires has a somewhat complex date format. From what I’ve seen in various implementations, this makes a difference; even small errors in generating the Expires value (e.g., omitting the leading ‘0’ from the hour) can cause downstream caches to misinterpret it. It happens more often than you think.

Compounding these errors are the caches themselves; in testing a variety of commercial and open source implementations, I found that a large number flout this requirement;

HTTP/1.1 clients and caches MUST treat other invalid date formats, especially including the value “0”, as in the past (i.e., “already expired”)."

So I suspect there may be a bug in ring or noir related to this, I am guessing ring. Perhaps the handling of JodaTime objects in ring has a bug somewhere.

I am going to stick with :max-age for now :)

Nick Bauman

unread,
Oct 29, 2012, 12:52:33 PM10/29/12
to clj-...@googlegroups.com
Thanks for figuring this out: very helpful to the community.

Geo

unread,
Oct 29, 2012, 6:10:22 PM10/29/12
to clj-...@googlegroups.com, jre...@weavejester.com
I got help on the ring discussion group. In order to make :expires work properly you need to explicitly include [ring "1.1.6"] in your project.clj.

More info here:

https://groups.google.com/forum/?fromgroups=#!topic/ring-clojure/CgK3xHiDbQg
Reply all
Reply to author
Forward
0 new messages