由于您提到的原因,您不应该通过IP地址识别个人 - 多个用户可以在同一个网络上。而应使用sessions或cookies来存储此信息。由于您希望将这些信息存储在用户关闭浏览器窗口时的位置,因此应该使用cookie,因为它们更持久。
如何购物车的详细信息存储在cookie可能看起来像这样的例子:
<?php
setcookie("cart", "item, another item, yet another item", time()+3600*24*365*10, '/');
?>
然后你可以使用explode
让所有的物品。
<?php
foreach (explode(",", $_COOKIE['cart']) as $item) {
echo trim($item); // Will output each item in the cookie cart
}
?>
另外,如果你想每个项目存储在一个单独的cookie,你也可以做这样的:
<?php
// It doesn't matter what you name the cookies in this case as you will be looping through all of them, you just need a unique string
setcookie("item 1", "item name 1", time()+3600*24*365*10, '/');
setcookie("item 2", "item name 2", time()+3600*24*365*10, '/');
setcookie("item 3", "item name 3", time()+3600*24*365*10, '/');
// Need page reload to access cookies
foreach ($_COOKIE as $item) {
echo $item;
}
?>
关于cookies重要提示:您可以不访问一个cookie在你设定的同时;您必须在两者之间重新加载页面。
来源
2015-08-15 05:20:46
Tim
Afaik其他网站为此使用会话 – bish