简体   繁体   English

设置cookie在Rails中的到期时间

[英]Setting expiry time of cookie in rails

I need to set a cookie which expires after 4 hours. 我需要设置一个cookie,它会在4小时后失效。 Now, the users can reside in multiple timezones. 现在,用户可以位于多个时区。 So, if I set the cookie in rails and set expiry based on server time, would it lead to invalid expiry times for some users or does rails handle this issue or does browser cookies themselves handle this issue ? 因此,如果我将cookie设置在rails中并根据服务器时间设置了到期时间,它会导致某些用户的到期时间无效,还是rails处理此问题,或者浏览器cookie本身处理此问题? Should I set this cookie in javascript, so that user's browser time is taken to set the expiry? 我是否应该在JavaScript中设置此Cookie,以便花费用户的浏览器时间设置到期时间?

Thanks! 谢谢!

I think you want to expire cookie after 4 hours since it was set. 我认为您要在设置Cookie后4小时后使它过期。 You just need to specify time that should be ahead of 4 hours from current time. 您只需要指定比当前时间早4小时的时间即可。 There is no concern with timezone. 与时区无关。 Because 4 hours is same for all timezones :P 因为所有时区的4小时都是相同的:P

cookies[:login] = { value: "XJ-122", expires: 4.hour.from_now }

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM