class ActionDispatch::Cookies
only HTTP. Defaults to false
.
* :httponly
- Whether this cookie is accessible via scripting or
Default is false
.
* :secure
- Whether this cookie is only transmitted to HTTPS servers.
* :expires
- The time at which this cookie expires, as a Time object.
For example, to share cookies between user1.lvh.me and user2.lvh.me, set :tld_length
to 1.
set the TLD length when using a short (<= 3 character) domain that is being interpreted as part of a TLD.
* :tld_length
- When using :domain => :all
, this option can be used to explicitly
# for concrete domain names.
domain: %w(.example.com .example.org) # Allow the cookie
# domain and subdomains.
domain: :all # Allow the cookie for the top most level
domain: nil # Does not set cookie domain. (default):all
or Array
again when deleting cookies.
to :all
. Make sure to specify the :domain
option with
and want to share session with user.example.com set :domain
restrict to the domain level. If you use a schema like www.example.com<br>* :domain
- The domain for which this cookie applies so you can
of the application.
* :path
- The path for which this cookie applies. Defaults to the root
* :value
- The cookie’s value.
The option symbols for setting cookies are:
cookies.delete(:name, domain: ‘domain.com’)
}
domain: ‘domain.com’
expires: 1.year.from_now,
value: ‘a yummy cookie’,<br>cookies = {
Please note that if you specify a :domain when setting a cookie, you must also specify the domain when deleting the cookie:
cookies.delete :user_name
Example for deleting:<br><br>cookies.encrypted # => 45<br>cookies.signed # => “XJ-122”<br>JSON.parse(cookies) # => [47.68, -122.37]
cookies.size # => 2<br>cookies # => “david”
Examples of reading:<br><br>cookies.permanent.signed = “XJ-122”
# You can also chain these methods:<br><br>cookies.permanent = “XJ-122”
# Sets a “permanent” cookie (which expires in 20 years from now).<br><br>cookies.encrypted = 45
# It can be read using the encrypted method ‘cookies.encrypted`
# The cookie is signed by your app’s ‘secrets.secret_key_base` value.
# prevent users from reading and tampering with its value.
# Sets an encrypted cookie value before sending it to the client which<br><br>cookies.signed = current_user.id
# It can be read using the signed method `cookies.signed`
# The cookie is signed by your app’s ‘secrets.secret_key_base` value.
# Sets a signed cookie, which prevents users from tampering with its value.<br><br>cookies = { value: “XJ-122”, expires: 1.hour.from_now }
# Sets a cookie that expires in 1 hour.<br><br>cookies = JSON.generate([47.68, -122.37])
# Cookie values are String based. Other data types need to be serialized.<br><br>cookies = “david”
# This cookie will be deleted when the user’s browser is closed.
# Sets a simple session cookie.
Examples of writing:
the cookie object itself back, just the value it holds.
being written will be sent out with the response. Reading a cookie does not get
The cookies being read are the ones received along with the request, the cookies
Cookies are read and written through ActionController#cookies.
def call(env)
def call(env) request = ActionDispatch::Request.new env status, headers, body = @app.call(env) if request.have_cookie_jar? cookie_jar = request.cookie_jar unless cookie_jar.committed? cookie_jar.write(headers) if headers[HTTP_HEADER].respond_to?(:join) headers[HTTP_HEADER] = headers[HTTP_HEADER].join("\n") end end end [status, headers, body] end
def initialize(app)
def initialize(app) @app = app end