问题描述
在Rails 3中,将会话存储设置为CookieStore的默认值,将数据存储在cookie中和将数据存储在会话中有什么区别?
In Rails 3, what is the difference between storing data in a cookie and storing data in a session, with the session store set to the default of CookieStore?
例如
cookie[:foo] = 'bar'
# MyApp::Application.config.session_store :cookie_store, key: '_myapp_session'
session[:foo] = 'bar'
据我所知,两者最终都存储在客户端cookie中。
As far as I can tell, both end up stored in a client-side cookie.
您何时选择在另一个上使用?
When would you choose to use one over the other?
谢谢。
推荐答案
主要区别是使用 cookie [:foo] ='bar'
用户可以看到cookie的值,即'bar'
。当您使用 session [:foo] ='bar'
时,该值将由rails加密并存储在 _myapp_session
中Cookie。
The main difference is that when you use cookie[:foo] = 'bar'
the user is able to see the value for the cookie, i.e. 'bar'
. When you use session[:foo] = 'bar'
the value will be encrypted by rails and stored in the _myapp_session
cookie.
当您要存储的信息未绑定到Cookie时,您将使用 cookie []
格式。会话,例如当用户选择首选语言时。
You would use the cookie[]
format when the information you want to store is not bound to the session, e.g. when the users selects the preferred language.
当您要存储与当前会话相关的信息时,可以使用 session []
格式,例如用户的 id
。
You would use the session[]
format when you want to store information that is related to the current session, e.g. the id
of the the user.
这篇关于Cookie与CookieStore的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!