Rails 3 基于 Cookie 的会话问题

发布于 2024-09-19 12:53:16 字数 334 浏览 6 评论 0原文

在 Rails 3 中,默认的会话存储机制是 cookie_store。我认为这意味着会话哈希中的内容被序列化、编码并存储在浏览器的 cookie 中?这是否意味着服务器中没有存储任何(或很少)会话内容?

我遇到了一些问题,其中出现了 cookie 溢出错误,我假设是因为我不断添加到我的用户实例(也从 cookie 链接/获取)。

u = session[:user]
u.add_this lots_of_data

所以最终我得到了 cookie 溢出错误。

我的说法正确吗? Rails 3 中的会话是否完全存储在 cookie 中(默认情况下)?

With Rails 3, the default session storage mechanism is cookie_store. I assume that this means that the contents within the session hash are serialized, encoded and stored within a cookie in the browser? Does this mean that nothing (or very little) of the session is stored in the server?

I've had a few issues where I had a cookie overflow error and I'm assuming because I kept on adding to my user instance (which was also linked/fetched from the cookie).

u = session[:user]
u.add_this lots_of_data

so eventually I got a cookie overflow error.

Am I correct about this? Are sessions fully stored within cookies in Rails 3 (by default)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2024-09-26 12:53:16

是的,如果您使用 cookie 存储,会话数据将存储在 cookie 中。如果您想将其存储在服务器上,则需要使用另一个会话存储。

但是,如果您在会话中存储模型对象或“大量数据”,那么您很可能一开始就做错了。您的数据应该进入数据库,并且会话应该只包含检索它所需的信息。

在您的情况下,这意味着将用户 ID 存储在会话中,并从数据库加载用户到 before_filter 中。

Yes, if you use the cookie store, the session data is stored in the cookie. If you'd like to store it on the server, you will need to use another session store.

However, if you are storing model objects or "lots of data" in the session, you are most likely doing it wrong in the first place. Your data should go to the database, and the session should only contain as much information as you need to retrieve it.

In you case, this would mean to store the user id int he session, and load the user from the db in a before_filter.

哆啦不做梦 2024-09-26 12:53:16

是的你是对的。如果您继续向会话添加数据,则可能会出现问题。
但还有一些其他因素会影响它。

有一次,我遇到了 CookieOverflow 错误,原因是 flash[:notice] 消息。

如果您使用 flash[:notice] = "message" 然后重定向,则文本“message”将存储在 cookie 中。如果您传递的文本大小超过 4KB,您会收到“CookieOverflow”错误

Yes, you are right. The problem might come up if you keep on adding data to session.
But there are some other things that affect it.

Once, I ended up with CookieOverflow error, and the reason was the flash[:notice] messages.

If you use flash[:notice] = "message" and then redirect, the text "message" will be stored in the cookie. If the size of the text u pass is more than 4KBs, you get "CookieOverflow" error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文