I started using PHP a couple of months ago. For the sake of creating a login system for my website, I read about cookies and sessions and their differences (cookies are stored in the user's browser and sessions on the server). At that time, I preferred cookies (and who does not like cookies?!) and just said: "who cares? I don't have any good deal with storing it in my server", so, I went ahead and used cookies for my bachelor graduation project. However, after doin' the big part of my app, I heard that for the particular case of storing user's ID, sessions are more appropriate. So I started thinking about what would I say if the jury asks me why have you used cookies instead of sessions? I have just that reason (that I do not need to store internally information about the user). Is that enough as a reason? or it's more than that? Could you please tell me about advantages/disadvantages of using cookies for keeping User's ID?

感谢大家在StackOverflow!


当前回答

博士TL;

Criteria / factors Sessions Cookies
Epoch (start of existence) Created BEFORE an HTTP response Created AFTER an HTTP response
Availability during the first HTTP request YES NO
Availability during the succeeding HTTP requests YES YES
Ultimate control for the data and expiration Server administrator End-user
Default expiration Expires earlier than cookies Lasts longer than sessions
Server costs Memory Memory
Network costs None Unnecessary extra bytes
Browser costs None Memory
Security Difficult to hijack Easy to hijack
Deprecation None Now discouraged in favor of the JavaScript "Web Storage"

细节

优点和缺点是主观的。它们可能会导致一种两分法(对一些人来说是优势,但对另一些人来说是劣势)。相反,我列出了可以帮助你决定选择哪一个的因素。

第一个HTTP请求-响应期间的存在

假设您是服务器端人员,希望同时处理会话和cookie。第一次HTTP握手是这样的:

浏览器准备HTTP请求——SESSIONS:不可用;COOKIES:不可用 浏览器发送HTTP请求 服务器接收HTTP请求 服务器处理HTTP请求——SESSIONS:已存在;饼干:投 服务器发送HTTP响应 浏览器接收HTTP响应 浏览器处理HTTP响应——SESSIONS:不可用;饼干:存在

在步骤1中,浏览器不知道会话和cookie的内容。 在步骤4中,服务器可以有机会设置会话和cookie的值。

后续HTTP请求-响应期间的可用性

浏览器准备HTTP请求——SESSIONS:不可用;饼干:可用 浏览器发送HTTP请求 服务器接收HTTP请求 服务器处理HTTP请求——SESSIONS:可用;饼干:可用 服务器发送HTTP响应 浏览器接收HTTP响应 浏览器处理HTTP响应——SESSIONS:不可用;饼干:可用

有效载荷

假设在一个网页中,你加载了example.com托管的20个资源,这20个资源将携带关于cookie的额外字节信息。即使它只是一个CSS或JPG图像的资源请求,在通往服务器的途中,它仍然会在它们的头文件中携带cookie。对JPG资源的HTTP请求应该携带一堆不必要的cookie吗?

弃用

会话是不可替代的。对于cookie,在浏览器中存储数据有许多其他选项,而不是老式的cookie。

用户数据的存储

会话对于存储用户数据更安全,因为最终用户不能修改它,只能在服务器端进行设置。另一方面,cookie可能被劫持,因为它们只是存储在浏览器上。

其他回答

I will select Session, first of all session is more secure then cookies, cookies is client site data and session is server site data. Cookies is used to identify a user, because it is small pieces of code that is embedded my server with user computer browser. On the other hand Session help you to secure you identity because web server don’t know who you are because HTTP address changes the state 192.168.0.1 to 765487cf34ert8ded…..or something else numbers with the help of GET and POST methods. Session stores data of user in unique ID session that even user ID can’t match with each other. Session stores single user information in all pages of one application. Cookies expire is set with the help of setcookies() whereas session expire is not set it is expire when user turn off browsers.

我个人同时使用cookie和session。

Cookies只在用户点击“记住我”复选框时使用。而且cookie是加密的,数据只在服务器上解密。如果有人试图编辑cookie,我们的解密器能够检测到它并拒绝请求。

我看到很多网站的登录信息存储在cookie中,任何人都可以简单地在cookie中更改用户的id和用户名来访问任何人的帐户。

谢谢,

博士TL;

Criteria / factors Sessions Cookies
Epoch (start of existence) Created BEFORE an HTTP response Created AFTER an HTTP response
Availability during the first HTTP request YES NO
Availability during the succeeding HTTP requests YES YES
Ultimate control for the data and expiration Server administrator End-user
Default expiration Expires earlier than cookies Lasts longer than sessions
Server costs Memory Memory
Network costs None Unnecessary extra bytes
Browser costs None Memory
Security Difficult to hijack Easy to hijack
Deprecation None Now discouraged in favor of the JavaScript "Web Storage"

细节

优点和缺点是主观的。它们可能会导致一种两分法(对一些人来说是优势,但对另一些人来说是劣势)。相反,我列出了可以帮助你决定选择哪一个的因素。

第一个HTTP请求-响应期间的存在

假设您是服务器端人员,希望同时处理会话和cookie。第一次HTTP握手是这样的:

浏览器准备HTTP请求——SESSIONS:不可用;COOKIES:不可用 浏览器发送HTTP请求 服务器接收HTTP请求 服务器处理HTTP请求——SESSIONS:已存在;饼干:投 服务器发送HTTP响应 浏览器接收HTTP响应 浏览器处理HTTP响应——SESSIONS:不可用;饼干:存在

在步骤1中,浏览器不知道会话和cookie的内容。 在步骤4中,服务器可以有机会设置会话和cookie的值。

后续HTTP请求-响应期间的可用性

浏览器准备HTTP请求——SESSIONS:不可用;饼干:可用 浏览器发送HTTP请求 服务器接收HTTP请求 服务器处理HTTP请求——SESSIONS:可用;饼干:可用 服务器发送HTTP响应 浏览器接收HTTP响应 浏览器处理HTTP响应——SESSIONS:不可用;饼干:可用

有效载荷

假设在一个网页中,你加载了example.com托管的20个资源,这20个资源将携带关于cookie的额外字节信息。即使它只是一个CSS或JPG图像的资源请求,在通往服务器的途中,它仍然会在它们的头文件中携带cookie。对JPG资源的HTTP请求应该携带一堆不必要的cookie吗?

弃用

会话是不可替代的。对于cookie,在浏览器中存储数据有许多其他选项,而不是老式的cookie。

用户数据的存储

会话对于存储用户数据更安全,因为最终用户不能修改它,只能在服务器端进行设置。另一方面,cookie可能被劫持,因为它们只是存储在浏览器上。

Session和Cookie不是一回事。

会话用于存储来自web页面的信息。通常情况下,网页没有存储这些信息的内存。但是使用我们可以保存必要的信息。

但Cookie是用来识别用户的。使用cookie,我们可以存储数据。它是存储在用户web浏览器中的一小部分数据。当用户下次浏览时,浏览器将cookie数据信息发回服务器,以获取之前的活动。

致谢:Session和Cookie

这个概念是跨页面加载为web访问者存储持久数据。cookie直接将其存储在客户机上。会话使用cookie作为某种密钥,与存储在服务器端的数据相关联。

最好使用会话,因为实际值对客户端隐藏,您可以控制数据何时过期并失效。如果它完全基于cookie,用户(或黑客)可以操纵他们的cookie数据,然后向您的站点发送请求。

编辑:我不认为使用cookie有任何好处,除了简单。这样看……用户有任何理由知道他们的ID号吗?通常我会说不需要,用户不需要这些信息。在需要知道的基础上,提供信息应该受到限制。如果用户将cookie更改为不同的ID,应用程序将如何响应?这是一个安全隐患。

在会话盛行之前,我基本上有自己的实现。我在客户机上存储了一个唯一的cookie值,并将我的持久数据与该cookie值一起存储在数据库中。然后在页面请求中,我匹配这些值并获得持久数据,而不让客户端控制这些数据。