【发布时间】:2018-08-09 07:31:14
【问题描述】:
我创建了 $_SESSION['cart'] 的购物车名称,并为用户创建了 $_SESSION['user']。当我点击添加到购物车时,相同的产品会显示给所有用户。我想知道如何将特定的 user_id 链接到购物车会话。我是 php 的初学者。请帮帮我。
这里是代码
if(isset($_SESSION["cart"]))
{
$item_array_id = array_column($_SESSION["cart"], "item_id");
if(!in_array($_GET['id'], $item_array_id)){
$count = count($_SESSION["cart"]);
$item_array = array(
'item_id' => $_GET['id'],
'item_name' => $_POST['hidden_name'],
'item_price' => $_POST['hidden_price'],
'item_image' => $_POST['hidden_image']
);
$_SESSION["cart"][$count] = $item_array;
}
}
else{
$item_array = array(
'item_id' => $_GET['id'],
'item_image' => $_POST['hidden_image'],
'item_name' => $_POST['hidden_name'],
'item_price' => $_POST['hidden_price']
);
$_SESSION["cart"][0] = $item_array;
}
}
【问题讨论】:
-
如果你使用的是php的默认会话设置,那是不可能的;会话对于访问者的浏览器会话是唯一的。你还在做什么,你是否也在使用数据库来存储购物车?
-
不,我没有使用数据库。我怎样才能使所有用户的唯一会话
-
每个访问者的会话总是独一无二的,您无需为此做任何事情。
-
那么我能做些什么来解决这个问题
-
任何解决方案兄弟