简体   繁体   English

$ _SESSION v。$ _COOKIE

[英]$_SESSION v. $_COOKIE

I learned about $_SESSION about several weeks ago when creating a login page. 几周前,我在创建登录页面时了解了$_SESSION I can successfully login and use it with variables. 我可以成功登录并将其与变量一起使用。 Currently I am trying to understand $_SESSION and $_COOKIE . 目前我正在尝试了解$_SESSION$_COOKIE Please correct me if I am wrong, I can use $_SESSION when logging in and moving around pages. 如果我错了请纠正我,我可以在登录和移动页面时使用$_SESSION With $_COOKIE , it is used to remember when I last visit and preferences. 使用$_COOKIE ,它用于记住我上次访问和偏好的时间。

Another thing involving cookies is that when websites use advertisements (for example: Google AdSense), they use the cookies to track when visitor click on a advertisement, right? 涉及cookie的另一个问题是,当网站使用广告(例如:Google AdSense)时,他们会使用Cookie来跟踪访问者点击广告的时间,对吗?

I can use both ( $_SESSION & $_COOKIE )? 我可以同时使用( $_SESSION$_COOKIE )? I read somewhere that you can store the session_id as value for the cookie. 我在某处读到你可以将session_id存储为cookie的值。

Also, I read about security which let to me finding this: What do I need to store in the php session when user logged in? 另外,我读到了安全性,让我发现这一点: 当用户登录时,我需要在php会话中存储什么? . Is using session_regenerate_id good for when a user comes back to the site? 当用户回到网站时,是否使用session_regenerate_id

And this: How to store a cookie with php involving uniqid . 这个: 如何使用涉及uniqid php存储cookie

For those wanting to know about the login, I use email and password. 对于那些想要了解登录的人,我使用电子邮件和密码。 That way the user can be able to change their username. 这样,用户就可以更改其用户名。

I look forward to learning more about these two from anybody who would like to share their knowledge about it. 我期待从任何想要分享他们知识的人那里学到更多关于这两个的知识。 If I asked too many question, you can just answer the one that you have more experience with. 如果我问了太多问题,你可以回答你有更多经验的问题。

If you need more information, just ask since I might have forgotten to include something. 如果您需要更多信息,请询问,因为我可能忘记包含一些内容。

Thank You. 谢谢。


Found this: What risks should I be aware of before allowing advertisements being placed on my website? 发现: 在允许在我的网站上放置广告之前,我应该注意哪些风险?

In simple terms, $_SESSION and $_COOKIE are different. 简单来说, $_SESSION$_COOKIE是不同的。 Both are php globals but cookies are used without a language limitation. 两者都是php全局,但使用cookie时没有语言限制。 $_SESSION is all about storing the data in the server while storing the session ID as a cookie. $_SESSION就是将数据存储在服务器中,同时将会话ID存储为cookie。 $_COOKIE is the cookies that browser sends to the server. $_COOKIE浏览器发送给服务器的cookie。 This is the major difference. 这是主要的区别。 Sessions don't work if the client browser has cookies disabled. 如果客户端浏览器禁用了cookie,则会话不起作用。

- Security - - 安全 -

If you checked request headers that your browser sends, you will notice that each request has cookie information in it. 如果您检查了浏览器发送的请求标头,您会注意到每个请求都包含cookie信息。 They can be tracked by snipping your network communication. 可以通过剪切网络通信来跟踪它们。 Anyone with a better tools can edit cookie data. 拥有更好工具的任何人都可以编辑cookie数据。 never use cookies to store passwords! 永远不要使用cookie来存储密码! If you use sessions, passwords are in the server and only the session id cookie will stored be in the client, reducing the security problem. 如果您使用会话,则密码位于服务器中,并且只有会话ID cookie将存储在客户端中, 从而减少了安全问题。 Chuck Norris still can hijack a session. 查克诺里斯仍然可以劫持一个会议。

- Performance - - 表现 -

If you store 5 cookies in the browser with 200 bytes in it, that cost ~1 KB of data on each and every request no matter if it's a jpg file or a page that actually needs the cookie information. 如果您在浏览器中存储5个cookie,其中包含200个字节,那么无论是jpg文件还是实际需要cookie信息的页面, 每个请求的数据大约需要1 KB。 So this directly affects how fast your site can perform to the end user. 因此,这会直接影响您的网站对最终用户的执行速度。

if you use sessions, server has this 1 KB data while the client sends the session ID in each page request. 如果使用会话,则服务器具有此1 KB数据,而客户端在每个页面请求中发送会话ID。 You can be clever by shifting static files to another cookie-less domain. 通过将静态文件转移到另一个无cookie的域,您可以很聪明。

- Lifetime - - 一生 -

Sessions gets cleared on timely basis. 会议及时得到批准。 So if you want to save something for a long time, use cookies instead. 因此,如果您想长时间保存某些内容,请使用Cookie。 "remember me" functionality of most sites works this way (still, it doesn't store the password. Just the session information - not to confuse with session ID). “记住我”大多数网站的功能都是这样工作的(但是,它不存储密码。只是会话信息 - 不要与会话ID混淆)。

Bottom line, sessions and cookies are different types. 底线,会话和cookie是不同的类型。 session is relatively secure and server side storage. 会话是相对安全的和服务器端存储。 gets cleared often. 经常清理。 Cookies can have a larger lifespan but it affects performance (not CPU/RAM -- load times) unless you don't keep that in mind. Cookie可以有更长的使用寿命,但它会影响性能(不是CPU / RAM - 加载时间),除非你不记得这一点。 It's extremely strange if there is a reason to store 1 KB as cookies though. 如果有理由将1 KB存储为cookie,那就太奇怪了。

Never trust user input that comes through $_GET/POST . 永远不要相信通过$_GET/POST来的用户输入。 Do the same care for $_COOKIE as well. 也为$_COOKIE做同样的照顾。 And there is session hijacking. 还有会话劫持。 Someone can guess someone else's session ID although it's nearly impossible to do. 有人可以猜到其他人的会话ID,尽管这几乎是不可能的。 So use some validation at the server side first. 因此,首先在服务器端使用一些验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM