Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set start date and expiration date for Rails cookies

People also ask

Can you set an expiration date and time for a cookie?

You can extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the 'expires' attribute to a date and time.

How do you set the expiration date on cookies?

Set an expiration date for a cookie This can be done easily by adding expires=expirationDate in UTC separated by semicolon from the name=value, as seen in the following example: document. cookie = "username=Max Brown; expires=Wed, 05 Aug 2020 23:00:00 UTC"; document.

How do you set cookies to expire in 30 days?

Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the cookie will expire at the end of the session (when the browser closes). The path on the server in which the cookie will be available on.

How do I set cookies to expire at end of session?

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.


Excerpts from Rails 5 documentation:

Cookies are read and written through ActionController#cookies.

The cookies being read are the ones received along with the request, the cookies being written will be sent out with the response. Reading a cookie does not get the cookie object itself back, just the value it holds.

Examples of writing:

# Sets a simple session cookie.
# This cookie will be deleted when the user's browser is closed.
cookies[:user_name] = "david"

# Sets a cookie that expires in 1 hour.
cookies[:login] = { value: "XJ-122", expires: 1.hour }

# Sets a cookie that expires at a specific time.
cookies[:login] = { value: "XJ-122", expires: Time.utc(2020, 10, 15, 5) }

# Sets a "permanent" cookie (which expires in 20 years from now).
cookies.permanent[:login] = "XJ-122"

[...]

The option symbols for setting cookies are:

  • :expires - The time at which this cookie expires, as a Time or ActiveSupport::Duration object.

[...]


your question might be related to this question: How to I dynamically set the expiry time for a cookie-based session in Rails

one of the comments points to Deprecating SlideSessions:

"..If you need to set expiration period for sessions through all controllers in your application, simply add the following option to your config/intializers/session_store.rb file:

:expire_after => 60.minutes

If you need to set different expiration time in different controllers or actions, use the following code in action or some before_filter:

request.session_options = request.session_options.dup
request.session_options[:expire_after]= 5.minutes
request.session_options.freeze

Duplication of the hash is needed only because it is already frozen at that point, even though modification of at least :expire_after is possible and works flawlessly..."

I hope that helps. :)


It's worth noting that as of right now it is impossible to set a start time for a cookie. A cookie set is always active immediately.