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!


当前回答

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.

其他回答

SESSIONS ENDS WHEN USER CLOSES THEIR BROWSER,

COOKIES END DEPENDING ON THE LIFETIME YOU SET FOR IT. SO THEY CAN LAST FOR YEARS

这是你选择的主要区别,

如果你想让id长时间被记住,那么你需要使用cookie;否则,如果你只是想让网站识别用户的访问,那么只有会话是可行的。

会话存储在php服务器将生成的文件中。为了记住哪个文件是针对哪个用户的,php还将在用户的浏览器上设置一个cookie,保存这个会话文件id,这样在用户下次访问时,php将读取这个文件并重新加载会话。

现在php默认每隔一段时间清除会话,并且会话的命名约定使其自动过期。此外,一旦浏览器关闭或历史记录被清除,浏览器将不会保留保存会话id的cookie。

值得注意的是,现在的浏览器还支持另一种存储引擎,如LocalStorage, SessionStorage和其他webdb引擎,javascript代码可以使用这些引擎将数据保存到您的计算机中以记住您。例如,如果你打开Facebook内部的javascript控制台,并输入“localStorage”,你会看到Facebook使用的所有变量来记住你没有cookie。

我个人同时使用cookie和session。

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

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

谢谢,

简短的回答

按优先级排序的规则:

规则1。永远不要相信用户输入:cookie是不安全的。对敏感数据使用会话。 规则2。如果用户关闭浏览器时必须保留持久数据,则使用cookie。 规则3。如果用户关闭浏览器时不需要保留持久数据,则使用会话。 规则4。阅读详细的答案!

来源:https://www.lucidar.me/en/web-dev/sessions-or-cookies/


详细的回答

饼干

cookie存储在客户端(在访问者的浏览器中)。 cookie并不安全:读取和写入cookie内容非常容易。 在使用cookie时,您必须根据欧洲法律(GDPR)通知访问者。 过期时间可以设置,但用户或浏览器可以更改它。 用户(或浏览器)可以(被设置)拒绝使用cookie。

会话

Sessions are stored on the server side. Sessions use cookies (see below). Sessions are safer than cookies, but not invulnarable. Expiration is set in server configuration (php.ini for example). Default expiration time is 24 minutes or when the browser is closed. Expiration is reset when the user refreshes or loads a new page. Users (or browser) can (be set to) decline the use of cookies, therefore sessions. Legally, you also have to notify visitors for the cookie, but the lack of precedent is not clear yet.

适当的选择

会话使用cookie!会话数据存储在服务器端,但UID存储在客户端cookie中。它允许服务器将给定用户与正确的会话数据匹配。UID是受保护的,很难被破解,但不是无懈可击的。对于敏感操作(更改电子邮件或重置密码),不要依赖会话或cookie:询问用户密码以确认操作。

敏感数据永远不应该存储在cookie(电子邮件、加密密码、个人数据……)请记住,数据存储在外国计算机上,如果计算机不是私有的(教室或公共计算机),其他人可能会读取cookie内容。

Remember-me数据必须存储在cookie中,否则当用户关闭浏览器时数据将丢失。但是,不要将密码或用户个人数据保存在“remember-me”cookie中。将用户数据存储在数据库中,并将此数据与存储在cookie中的加密ID /密钥对链接起来。

在考虑了前面的建议之后,下面的问题最终是什么可以帮助您在cookie和会话之间进行选择:

当用户关闭浏览器时,持久化数据必须保留吗?

如果答案是肯定的,请使用cookie。 如果答案是否定的,就使用会话。

cookie和Sessions用于存储信息。cookie只存储在客户端机器上,而会话存储在客户端和服务器上。

会话

会话在服务器上的临时目录中创建一个文件,其中存储了注册的会话变量及其值。在访问期间,网站上的所有页面都可以使用这些数据。

当用户关闭浏览器或离开站点时,会话结束,服务器将在预定的时间(通常为30分钟)后终止会话。

饼干

cookie是存储在客户端计算机上的文本文件,用于跟踪使用目的。服务器脚本向浏览器发送一组cookie。例如姓名、年龄或身份证号等。浏览器将这些信息存储在本地机器上以备将来使用。

当浏览器下次向web服务器发送任何请求时,它将这些cookie信息发送到服务器,服务器使用这些信息来识别用户。

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

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

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

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