douzhuo1858 2018-02-14 15:35
浏览 40
已采纳

在高流量站点上使用PHP会话对象的推荐方法是什么?

I am currently working on a website that has high traffic by my standards. It's a webshop and has a few (serialized) objects in it's session.

E.g.

$_SESSION['shoppingcart'] = new Shoppingcart();

And this object is later used throughout the code. So far no problem. The session duration is 1 hour at the moment and is extended when users keep it alive by making requests.

Now for the (probably well known) problem: When making a change to the Shoppingcart class in the example there is a chance that the change breaks the deserializing of the object. What can we do to prevent this problem? What is the recommended approach?

  • We can keep values in the database and only use a primitive int inside the session to identify the primary key.

  • We can use some array conversion from the object.

  • ?

  • 写回答

1条回答 默认 最新

  • douxuanma4357 2018-02-14 15:42
    关注

    There is a good chance there is an expert here that has a better answer, but for now I will share what I found.

    Given all these potential problems, I strongly advise against storing objects in the session. If you want to persist the logged in user, instead of storing an instance of a User class in $_SESSION, just store the user ID and populate the user object from the database or cache. It’s a little more work than letting PHP magically handle everything for you, but your application will be much more stable and portable without object serialization.

    source: https://www.phparch.com/2018/01/php-sessions-in-depth/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog