12

I need to use a cookie for my Sinatra application. If I use the simpliest method is works:

response.set_cookie('my_cookie', 'value_of_cookie')

but I need some options such as domain and expire date so I try this:

response.set_cookie("my_cookie", {:value => 'value_of_cookie', :domain => myDomain, :path => myPath, :expires => Date.new})

does not work. No cookie is made. I need this so much....

Please help... thanks!

claudiut
  • 1,643
  • 4
  • 15
  • 20
  • How are you determining that no cookie is made? Browser cookie store? Later use in your application? Reading the traffic on the wire with wireshark? Does the `:expire => Date.new` mean the cookie should expire "this very instant"? – sarnold Feb 22 '11 at 12:34
  • I use a cookie addon in Firefox. – claudiut Feb 22 '11 at 12:59

1 Answers1

17

The documentation on http://sinatra-book.gittr.com/#cookies says to use the set_cookie helper, but in newer versions of Sinatra (at least from 1.2.0+ and possibly earlier), you should use response.set_cookie to set cookies.

response.set_cookie("my_cookie", :value => "value_of_cookie",
                    :domain => myDomain,
                    :path => myPath,
                    :expires => Date.new(2020,1,1))
cookie = request.cookies["my_cookie"]

SUMMARY

don't set localhost as a domain for your cookies because you need to set it to "" or FALSE

Community
  • 1
  • 1
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • 1
    set_cookie is not a helper anymore. Now it's response.set_cookie(). This should be correct but does not work: http://www.gittr.com/index.php/archive/sinatra-cookie-handling-in-0-9-4/ – claudiut Feb 22 '11 at 12:46
  • 3
    This works: response.set_cookie('my_cookie', { :value => 'abc', :path => request.path, :expires => Time.now + (60 * 60 * 24 * 30) }). It expires after a month. But why the :domain parameter doesn't work. :domain => request.host would be 'localhost' in my case. If I add that parameter it does not work.... but why? – claudiut Feb 22 '11 at 13:23
  • `request.path` will not return a domain!!! for me works perfect for `response.set_cookie("my_cookie", :value => "1234", :domain => ".smackaho.st")` – fl00r Feb 22 '11 at 13:24
  • Yes my mistake with request.path... I know. But why doesn't request.host work? – claudiut Feb 22 '11 at 13:25
  • try `request.env['SERVER_NAME']` or `request.env['HTTP_HOST']` – fl00r Feb 22 '11 at 13:27
  • Doesn't work unfortunately... Maybe doesn't work with localhost? – claudiut Feb 22 '11 at 13:33
  • what does it return? are you testing it on localhost? – fl00r Feb 22 '11 at 13:34
  • Yes, on localhost. If I use request.env['SERVER_NAME'] or request.env['HTTP_HOST'] or request.host the cookie is not created. – claudiut Feb 22 '11 at 13:36
  • Oh :) the reason is your localhost :). You can't set domain as localhost. Try run this url: http://test.smackaho.st:4567 just change port to what you are running your application – fl00r Feb 22 '11 at 13:39
  • Actually out of localhost even `request.host` will works perfect :) – fl00r Feb 22 '11 at 13:42
  • 1
    read this about setting `localhost` as a domain for cookie: http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain `when working on localhost (!) the cookie-domain must be set to "" or NULL or FALSE instead of "localhost"` – fl00r Feb 22 '11 at 13:43
  • I tested on some free domain of mine. Indeed.... it was because of localhost :)). So much struggle for this one. Thanks for your time :). I hope this will be useful for others, too. – claudiut Feb 22 '11 at 13:49