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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度