我什么时候应该使用会话变量而不是 cookie? [英] When should I use session variables instead of cookies?

查看:34
本文介绍了我什么时候应该使用会话变量而不是 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会话变量和 cookie 对我来说似乎非常相似.我了解技术差异,但您如何决定何时使用一种还是另一种?

Session variables and cookies seem very similar to me. I understand the technical differences, but how do you decide when to use one vs. the other?

推荐答案

  • 会话存储在服务器上,这意味着客户端无法访问您存储的有关它们的信息.会话数据存储在您的服务器上,不需要与每个页面一起完整传输;客户端只需要发送一个 ID,数据就会从服务器加载.

    • Sessions are stored on the server, which means clients do not have access to the information you store about them. Session data, being stored on your server, does not need to be transmitted in full with each page; clients just need to send an ID and the data is loaded from the server.

      另一方面,cookie 存储在客户端.它们可以长时间耐用,并且当您拥有一组 Web 服务器时,可以让您更顺畅地工作.但是,与会话不同的是,存储在 cookie 中的数据会随每个页面请求完整传输.

      On the other hand, cookies are stored on the client. They can be made durable for a long time and would allow you to work more smoothly when you have a cluster of web servers. However, unlike sessions, data stored in cookies is transmitted in full with each page request.

      避免在 cookie 中存储数据

      Avoid storing data in cookies

      • 它可以被最终用户看到、读取和操纵,或者被那些有恶意的人拦截.除了session_id"之外,您不能信任 cookie 中的任何数据.
      • 它会增加您的带宽,如果您为每个用户的每个页面请求添加 1k 数据,则可能会将您的带宽增加 10-15%.从 $$ 的角度来看,这可能并不昂贵,但从性能的角度来看可能会如此.它会有效地将每台服务器上的带宽减少 10-15%,也就是说,它可能会导致您需要更多的服务器.

      您可以在会话数据中存储什么取决于您拥有的数据量和用户数量.no_of_users * size_of_session_data 必须小于服务器上的可用内存.

      What you can store in session data depends on the amount of data and number of users you have. no_of_users * size_of_session_data must be less than the free memory available on your server.

      这篇关于我什么时候应该使用会话变量而不是 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆