douyi7283 2012-09-24 16:58
浏览 70
已采纳

购物车持久性:$ _SESSION或浏览器cookie?

On an e-commerce site with no username/login to persist cart data, would it be better to use the PHP $_SESSION variable or a browser cookie to persist items in the shopping cart? I am leaning toward $_SESSION since cookies can be disabled, but would like to hear thoughts from you.

Thank you in advance for your consideration.

  • 写回答

6条回答 默认 最新

  • douya6606 2012-09-24 17:17
    关注

    Neither

    No large sites would dare store a user's cart in a session or cookie - that data is just to valuable.

    What customers are buying, when they select items, how many they purchase, why they don't finish the checkout, etc.. are all very, very important to your business.

    Use a database table to store this information and then link it to the user's session. That way you don't lose the information and you can go back and build statistics based on users carts or solve problems with your checkout process.

    Log everything you can.

    Database Schema

    Below is a simplified example of how this might look at the database level.

    user {
        id
        email
    }
    
    product {
        id
        name
        price
    }
    
    cart {
        id
        product_id
        user_id
        quantity
        timestamp    (when was it created?)
        expired      (is this cart still active?)
    }
    

    You might also want to split the cart table out into more tables so you can track revisions to the cart.

    Sessions

    Normal PHP Sessions consist of two parts

    1. The data (stored in a file on the server)
    2. A unique identifier given to the user agent (browser)

    Therefore, it's not $_SESSION vs $_COOKIE - it's $_SESSION + $_COOKIE = "session". However, there are ways you can modify this by using a single encrypted cookie which contains the data (and therefore you don't need an identifier to find the data). Another common approach is to store the data in memcached or a database instead of the filesystem so that multiple servers can access it.

    What @Travesty3 is saying is that you can have two cookies - one for the session, and another that is either a "keep me logged in" cookie (which exists longer than the session cookie), or a copy of the data inside separate cookie.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么