我应该何时使用会话变量而不是Cookie

我应该何时使用会话变量而不是Cookie

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

问题描述

会话变量和cookie看起来与我非常相似。

解决方案


  • 会话存储在服务器上,这意味着客户端无法访问您存储的有关这些会话的信息。存储在服务器上的会话数据不需要与每个页面一起完整传输;


  • 另一方面,Cookies存储在客户端上。客户端只需要发送ID即可从服务器加载数据。它们可以长期保持耐用,并允许您在拥有一组Web服务器时更顺利地工作。但是,与会话不同,存储在Cookie中的数据会在每个页面请求中完整传输。

    读取和操纵,或被具有恶意意图的人拦截。您不能信任Cookie中的任何数据(session_id除外)。

  • 如果您为每个用户每页请求添加1k的数据,则可能会增加您的带宽减少10-15%。从$$角度来看,这可能不是很昂贵,但可以从性能的角度看。


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



    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?

    解决方案

    • 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.

    • 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.

    • Avoid storing data in cookies

      • it can be seen, read and manipulated by the end user, or intercepted by those with nefarious intent. You can't trust any data in cookies, except for the "session_id".
      • It increases your bandwidth, if you add 1k of data per page request per user, that might increase your bandwith by 10-15%. This is perhaps not costly from a $$ perspective, but it could be from a performance perspective. It effectively would decrease your bandwidth on a per server by 10-15%, ie It might cause you to need more servers.
    • 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 free memory available on your server.

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

    09-02 14:42