我有一个存储 2 个数组的会话,$_SESSION["info"] 和 $_SESSION["cart"],现在当用户注销信息会话时,信息会话被销毁,因此未设置($_SESSION["信息"],但购物车会话仍然存在。信息会话存储用户的 ID,现在我尝试从信息数组中检索该用户的 ID,并在用户注销时将其存储在 cookie 中,因此当用户重新登录时,如果存储在会话中的用户 ID 与 cookie 用户 ID 匹配,它使用与该用户 ID 相关的购物车数组。
这是我到目前为止在注销中所拥有的.php:
$_COOKIE["userID"] = $_SESSION["info"]["id"];
unset($_SESSION["info"]);
在我的愿望清单.phtml中:
<?php
if (isset($_SESSION["cart"])) {
if (count($_SESSION["cart"])>0) {
echo '<br><br>';
echo '<table class="table table-hover"><thead><tr>';
echo '<th>Owner</th><th>Car</th><th>Price</th><th class="text-center">Action</th> </tr>';
echo '</thead><tbody>';
foreach ($_SESSION["cart"] as $key => $wish) {
echo '<tr><td>' . $wish["username"]. '</td><td>' . $wish["car"]. '</td><td>' .$wish["price"]. '</td><td class="text-center"><a href="/removecar.php?car='.$key. '"><button id="view" type="button" class="btn btn-danger">x</button></a></td></tr>';
}
echo '</tbody></table>';
}
else {
echo '<div class="alert alert-danger">You do not have any cars in your wishlist.</div>';
};
}
?>
有谁知道我应该怎么做?
$_COOKIE 是只读数组,你需要像 setcookie 一样使用 setcookie
setcookie('userID', $_SESSION["info"]["id"], time()+60*60*24*30, '/');
顺便说一句,您将无法将用户ID关联回会话,因此最好将整个购物车保存在数据库或cookie中