Question

I have a situation in which two cookies have the same name but slightly different domains (cookie1 has the domain example.com whereas cookie2 has sub-domain inclusive .example.com).

cookies[ :cookie_name ] = { :value => "test_value_cookie_1", :domain => "example.com" }
cookies[ :cookie_name ] = { :value => "test_value_cookie_2", :domain => ".example.com" }

I want to detect when both cookies exist, but unfortunately I can't out figure how to access a cookie by it's own domain. I can say

if cookies[ :cookie_name ].blank?

but how do I say

if cookies[ :cookie_name, :domain => ".example.com" ].blank?

or

if cookies[ :cookie_name ].domain[ ".example.com" ].blank?

I'm using "actionpack-2.3.11/lib/action_controller/cookies". I don't understand why you can set information like the domain, but not access it.

Was it helpful?

Solution

Apparently what I wanted to do was impossible. The HTTP spec only allows cookies to be accessed by name even though they can be set by domain:

When requesting a URL from an HTTP server, the browser will match the URL against all cookies and if any of them match, a line containing the name/value pairs of all matching cookies will be included in the HTTP request. Here is the format of that line: Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2 ... http://curl.haxx.se/rfc/cookie_spec.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top