且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我什么时候应该使用会话变量而不是 cookie?

更新时间:2023-11-28 09:36:46

  • 会话存储在服务器上,这意味着客户端无法访问您存储的有关它们的信息.会话数据存储在您的服务器上,不需要与每个页面一起完整传输;客户端只需要发送一个 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.