购物车持久性:$ _SESSION或浏览器Cookie? [英] Shopping cart persistence: $_SESSION or browser cookie?

查看:148
本文介绍了购物车持久性:$ _SESSION或浏览器Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有使用用户名/登录信息来保存购物车数据的电子商务网站上,最好使用PHP $ _SESSION变量或浏览器Cookie在购物车中保留商品?我倾向于$ _SESSION,因为cookies可以被禁用,但想听到你的想法。



提前感谢您的考虑。

解决方案



没有大型网站会将用户的购物车存储在会话或Cookie中 -



客户购买什么,他们选择的项目,他们购买了多少,为什么他们没有完成结帐等等。对您的业务非常重要。



使用数据库表来存储此信息,然后将其链接到用户的会话。这样,您不会丢失信息,您可以返回,并根据用户购物车或解决问题的结帐流程建立统计信息。



记录一切您可以。 / p>

数据库模式



下面是一个简化的例子,

 用户{
id
电子邮件
}

产品{
id
name
price
}

cart {
id
product_id
user_id
quantity
时间戳(何时创建?)
已过期(此购物车是否仍然有效?)
}


b $ b

您可能还希望将购物车表拆分成更多的表格,以便您可以跟踪购物车的修订。



会话



正常PHP会话由两部分组成


  1. 数据(存储在服务器上的文件中) / li>
  2. 提供给用户代理(浏览器)的唯一标识符

c $ c> $ _ SESSION vs $ _ COOKIE - $ _ SESSION $ _ COOKIE =session。但是,有一些方法可以通过使用包含数据的单个加密的cookie来修改它(因此,您不需要标识符来查找数据)。另一个常见的方法是将数据存储在memcached或数据库中,而不是文件系统,以便多个服务器可以访问它。



@ Travesty3说的是, 两个 Cookie - 一个用于会话,另一个是保持我登录Cookie(其存在的时间长于会话Cookie)或单独Cookie中的数据副本。 p>

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.

解决方案

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.

这篇关于购物车持久性:$ _SESSION或浏览器Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆