我应该在每个网络响应中返回 cookie 吗?
当用户登录我的网站时,它会返回一个 2 小时过期的 cookie。后续调用中不会返回 cookie,因此两小时后,即使用户仍在使用网站,cookie 也会过期,然后重定向到登录页面。
所以我想我知道解决方案,但是返回 cookie 并在每次调用中更新“过期”是一个好习惯吗?
干杯。
When a user login in my website, it returns a cookie with two hours expire. The cookie is not returned in following calls, so after two hours the cookie expires even when the user is still using the website, and then redirected to the login page.
So I think I know the solution, but is it a good practice return the cookie with the "expire" updated in every call?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可以归结为为什么不?它解决了超时问题,并且没有任何缺点。
唯一的副作用是传输 cookie 所需的额外带宽,但这完全可以忽略不计。如果您确实关心该带宽,则只需每n分钟重新发送一次cookie。
It boils down to why not? It solves the timeout problem, and has no drawbacks.
The only side effect is the additional bandwidth necessary to transfer the cookie, but this is completely negligible. If you do care about that bandwidth, only resend the cookie every n minutes.
在每个服务器响应中设置会话 cookie 并不是一件大事,特别是因为客户端已经在每个请求中将其发送到服务器。
但是,您可以做得更好。如果客户端携带的 cookie 必定会在 1 小时 50 分钟后过期,您可以向他们发送一个新的 cookie,该 cookie 设置为新的 2 小时过期日期。您可以轻松跟踪会话处理代码中何时设置客户端 cookie(因此必然会过期)。
It's not a huge deal to set a session cookie in every server response, especially since the client is already sending it to the server in every request.
However, you can do better than that. If the client comes in with a cookie that's bound to expire, say, less than 1 hour and 50 minutes from now, you can send them a new cookie that's set to a new, 2-hour expiration date. You can easily keep track when a client cookie is set (and is therefore bound to expire) in your session handling code.