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和Sessions用于存储信息。cookie只存储在客户端机器上,而会话存储在客户端和服务器上。

会话

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

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

饼干

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

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

请参见插图来比较cookie和Session的差异。

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。

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.

区分这两者的基本思路。

会话:

UID存储在服务器端(即服务器端) 更安全(因为1) 过期不能设置,会话变量将在用户关闭浏览器时过期。(现在php默认存储24分钟)

饼干:

UID存储在web浏览器上(即客户端) 不是很安全,因为黑客可以到达并获得你的信息(因为1) 可以设置过期时间(有关详细信息,请参阅setcookies())

当您需要存储短期信息/值时,例如用于计算、测量、查询等的变量时,会话是首选。

当您需要存储长期的信息/值时,例如用户的帐户(这样即使他们关闭计算机2天,他们的帐户仍然会登录),首选cookie。我想不出很多关于cookie的例子,因为它在大多数情况下都没有被采用。