简体   繁体   English

将购物车项目存储到Cookie和数据库中

[英]Storing shopping cart items into cookies and database

I am working on an e-commerce website. 我正在一个电子商务网站上。 When the user is not logged into my website and clicks on "Buy Now" button, I want to store this information into the cookie as well as in the database. 当用户未登录我的网站并单击“立即购买”按钮时,我想将此信息存储到cookie以及数据库中。 The table for the shopping cart looks like 购物车的表看起来像

SHOPPING_CART 
(
     sessionid   int(10), 
     itemid      int(10), 
     quantity    tinyint(10) unsigned
     date_added  datetime
);

Primary key is: (sessionid, itemid) 主键是: (sessionid, itemid)

When the user closes the browser then also the shopping cart items should be preserved. 当用户关闭浏览器时,也应保留购物车项目。 Now my question is the following: 现在我的问题是:

  1. When the user is not logged into my website, on what basis I should identify the user? 当用户未登录我的网站时,我应根据什么身份识别用户?
  2. Should I store the information using the IP address ? 我应该使用IP address存储信息吗? If yes then HOW? 如果是,那怎么办? In this case sessionid in the above mentioned table would be the IP address of the user. 在这种情况下,上述表格中的sessionid将是用户的IP地址。 Right? 对?
  3. Should I create a temporary session for each and every user who visits my website and then store the information? 我应该为访问我的网站的每个用户创建一个临时会话,然后存储信息吗? If yes then HOW? 如果是,那怎么办?
  4. How can the shopping cart items be preserved even when the user closes the browser window? 即使用户关闭浏览器窗口,如何保存购物车商品? Should I retrieve from database or cookie? 我应该从数据库还是cookie中检索?
  5. Any other better method to store and retrieval of the information? 还有其他更好的方法来存储和检索信息吗?

Note1 : I can use plenty of Shopping Cart softwares/codes/libraries available. 注意1 :我可以使用许多可用的购物车软件/代码/库。 But I want to know: How to identify the user? 但我想知道: 如何识别用户? And storing/retrieval of data. 以及存储/检索数据。

Note2 : The price of each item, ordering, shipping information all are stored in different tables. 注意2 :每个项目的价格,订购,运输信息都存储在不同的表中。

  1. All you can do is create a unique fake identity for the user 您所能做的就是为用户创建一个唯一的伪造身份
  2. No. Multiple users may have the same IP address, and a single user may change its IP address 否。多个用户可能具有相同的IP地址,并且一个用户可以更改其IP地址
  3. Yes. 是。 PHP will create a session for you as soon as you ask to start a session. 当您要求启动会话时,PHP将为您创建一个会话。 You must associate an identity with this session. 您必须将身份与此会话关联。 Just use a random number, or a UUID generator, or something like that to generate something unique and not easily guessable. 只需使用随机数或UUID生成器,或类似的东西即可生成唯一且不容易猜测的内容。 Then store the identity in a cookie so that when the user comes back some time later, you can re-associate his identity with the new session. 然后将身份存储在cookie中,以便用户稍后再回来时,可以将其身份与新会话重新关联。
  4. I would just store the identity in the cookie. 我只是将身份存储在cookie中。 A cookie only holds a small amount of information, and may be modified by the user without you knowing it. Cookie仅包含少量信息,用户可能会在不知情的情况下对其进行修改。
  5. If the users don't log in, I don't see any other way. 如果用户未登录,则看不到其他任何方式。

the only thing you have to do is set the sessionid into a client-cookie. 唯一要做的就是将sessionid设置为client-cookie。 if a customer returns and presents a sessionid cookie you update your cart table with his new sessionid (and set the new sessionid in the cookie). 如果客户返回并出示sessionid cookie,则您用他的新sessionid更新购物车表(并在cookie中设置新的sessionid)。

  1. session (that's what it is for) 会话(这就是它的目的)
  2. no 没有
  3. 'temporary session'? “临时会议”?
  4. the cart is in the database 购物车在数据库中
  5. better in what sense? 在什么意义上更好? secure? 安全? robust? 强大的? user friendly? 方便使用的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM