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!


当前回答

正如其他人所说,Sessions很聪明,而且在向客户端隐藏信息方面更有优势。

但是Cookie至少还有一个优势,你可以从Javascript访问你的Cookie(例如ngCookies)。使用PHP会话,您不能在PHP脚本之外的任何地方访问它。

其他回答

会话允许您像使用cookie一样存储单独的信息片段,但是数据存储在服务器上而不是客户机上。

我个人同时使用cookie和session。

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

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

谢谢,

实际上,session和cookie并不总是分开的。会话通常(但不总是)使用cookie。

你的问题在这里的其他问题中有一些很好的答案。因为你的问题是专门关于保存用户的IDU(或ID)的,我不认为它与其他问题完全相同,但他们的答案应该对你有帮助。

cookie vs会话

缓存VS会话VS cookie ?

会话和Cookie的区别是什么?

简短的回答

按优先级排序的规则:

规则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信息发送到服务器,服务器使用这些信息来识别用户。