0
具有存儲一個數組變量作爲cookie,然後加入在單獨的陣列到餅乾已經包含數組添加陣列存儲在cookie的陣列
$mysqli = mysqli_connect($db_host,$db_user,$db_pass,$db_base);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Displays a message saying product was added to the basket
$message = $_POST['product_name']." was added to the basket";
echo "<script>alert(".$message.")</script>";
// Sets the basket
$itemID = $_POST['product_id'];
$itemQuantity = $_POST['product_quantity'];
if ($itemQuantity > 0)
{
$items = [$itemID, $itemQuantity];
// Returns cookie value as array
$basket_array = (unserialize($_COOKIE['eg_basket']));
// Adds to array into cookie array
$basket = serialize(array_push($basket_array, $items));
// Sets basket back as cookie
setcookie('eg_basket', $basket); // will expire on browser close
// Displays message
echo "<h3 style='text-align:center'>".$_POST['product_name']." was added to the basket</h3>";
echo "<br/>";
echo "<p style='text-align:center'>Please click below to return to the previous page</p>";
}
else
{
echo "<h3 style='text-align:center'>ERROR: ".$_POST['product_name']." was not added to the basket, invalid quantity given</h3>";
}
echo "<form method='POST' action='product_info.php'><input style='display:none' type='number' name='product_id_POST' value='".$_POST['product_id']."'><input style='text-align:center' type='submit' value='Return'></form>";
// close the connection
mysqli_close($mysqli);
?>
我所tryng做的是創造一個問題將存儲產品ID陣列的cookie;以及數量
例如cookie = [[product_id,quantity],[product_id,quantity],.......]; 然而
即時通訊相當肯定這是不是這樣的,但是這是我使用創造了籃下的cookie,如果不存在的話(可能這就是爲什麼cookie將不接受任何新的價值的原因代碼
// Checks is cookie is already set - basket only
if (!isSet($_COOKIE['eg_basket']))
{
$basket = serialize([]);
setcookie('eg_basket', $basket); // will expire on browser close
}
感謝,任何幫助是極大的讚賞
公牛
我建議你從cookie更改爲會話,會更容易管理和更安全^ _ ^ – Aliceiw