电子商务网站的会话处理

发布于 2024-11-05 10:04:24 字数 173 浏览 0 评论 0原文

我正在用 PHP 开发一个电子商务网站,我想实现以下功能:如果用户未登录,他可以将产品添加到购物车,并且在登录后仍然拥有这些产品。此外,该功能必须以相反的方式工作:用户登录,然后将产品添加到购物车。

我认为执行此操作的一个好方法是使用会话 ID,但经过一些测试后发现这不是最好的方法。

有什么想法吗?

I'm working on a e-commerce website in PHP and I would like to implement the following feature: if a user is not logged in, he can add products to the shopping cart and still have those products after login. Also, the feature must work the other way around: a user is logging in and then he adds products to the shopping cart.

I thought that a good way to do this is by using the session id, but after doing some test it turned out that this isn't the best way to do it.

Any ideeas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任性一次 2024-11-12 10:04:24

为什么使用会话不是最好的方法?我想说是的。

您可以拥有一个单独的、基于会话的“非登录”购物车结构,这是普通购物车的精确副本。如果用户未登录,产品将存储在那里。

当用户登录时,您将未登录的购物车的内容与用户特定于用户的购物车中可能已有的任何商品合并。

该点也是处理产品选择可能引起的任何冲突的地方(例如,所选产品已存在于登录用户的购物车中)。

使用 @Codemwnci 推荐的 cookie 来存储产品或购物车 ID 也是一个好主意,因为它允许用户稍后返回并仍然拥有您可能想要的购物车内容。

相同的合并原则也适用于此,但需要额外检查 cookie 中的产品是否确实有效(它们可能在用户做出选择后已被删除,或者用户可能已更改 cookie)。

Why is using the session not the best way to do it? I'd say it is.

You could have a separate, session-based "non-logged in" shopping cart structure, an exact copy of the normal cart. If the user is not logged in, the products are stored there.

When the user logs in, you merge the non-logged in cart's contents with whatever items the user may already have in their user-speficic cart.

That point is also the place to deal with any conflicts that may arise from the product selection (like, a selected product being present in the logged in user's cart already).

Using cookies as recommended by @Codemwnci to either store the products, or a cart ID is a good idea too, because it allows the user to come back later and still have their cart contents, which you may want.

The same principle of merging will apply here as well, with an additional check whether the products in the cookie really are valid ones (they could have been removed since the user made their selection, or the user could have altered the cookie).

你如我软肋 2024-11-12 10:04:24

您可以使用cookies。只需在 cookie 中存储一个代表购物篮的唯一标识符即可。无论用户是否登录,Cookie 标识符仍然相同,因此数据将被持久保存。

You can use cookies. Just store a unique identifier in the cookie, that represents the shopping basket. Regardless of whether the user is logged in or out, the Cookie identifier will still be the same, therefore the data will be persisted.

明月夜 2024-11-12 10:04:24
session_start();
$_SESSION["cartitems"] = "1,2,3";
session_start();
$_SESSION["cartitems"] = "1,2,3";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文