Cookie 在运行时设置为空


Cookie is set to null when ran

让我先给你代码,并在下面解释:

<?php
$productID = $_POST['productID'];
$productAmount = $_POST['productAmount'];
//Declare variables
$cartNumItems = 0;
//Check if cookie exists and set variable if it does
if(isset($_COOKIE['cart'])){
    if($_COOKIE['cart'] != null && $_COOKIE['cart'] != ''){
        $cart = $_COOKIE['cart'];
    }
}
//Get array from cookie
if(isset($cart)){
    //Decode array from $cart variable and store it
    $cartArray = json_decode($cart);
    //Count number of items in $cart
    $cartAmount = count($cartArray);
    for($i = 0; $i < $cartAmount; $i++){
        //Check if $cart has the same product in it already
        if($productID == $cartArray[$i][0]){
            //Add to the amount of that product
            $cartArray[$i][1] += $productAmount;
            break;
        }
        //If it does not have the same product already, just add a new one
        else if($i == ($cartAmount - 1)){
            array_push($cartArray, array($productID, $productAmount));
            break;  
        };
    };
    //Recount number of items in $cart
    for($i = 0; $i < $cartAmount; $i++){
        $cartNumItems += $cartArray[$i][1];
    };
    //Encode $cart so it can be stored in cookie
    $cartRaw = json_encode($cartArray);
    //Create cookies
    setcookie('cart', $cartRaw, time() + (86400 * 7), '/');
    setcookie('cartNumItems', $cartNumItems, time() + (86400 * 7), '/');
    echo 'true';
}
else{
    //Create the info that needs to be put into cookie
    $cart = json_encode(
        array(
            array($productID, $productAmount)
        )
    );
    $cartArray = json_decode($cart);
    //Count and store the amount of items in cart array
    $cartAmount = count($cartArray);
    //Store amount of items in $cartNumItems variable
    for($i = 0; $i < $cartAmount; $i++){
        $cartNumItems += $cartArray[$i][1];
    };
    //Create cookies
    setcookie('cart', $cart, time() + (86400 * 7), '/');
    setcookie('cartNumItems', $cartNumItems, time() + (86400 * 7), '/');
    echo 'true';
};
?>

第一次运行此代码时,不会设置"购物车"cookie。因此,它在检查变量"$cart"是否设置时运行 big else 语句。else 语句工作正常,并执行它应该执行的操作。它创建一个 2D 数组,然后使用所需信息创建 cookie。

代码第二次运行时,应运行 if 语句。我相信确实如此。"购物车"cookie(又名"$cart"变量)设置为 null。然后将"cartNumItems"cookie(又名"$cartNumItems"变量)设置为 0。这是不应该做的。我只是找不到它在哪里这样做,或者为什么这样做。

如果对具体是什么问题代码有任何混淆,这就是问题代码(看起来):

//Get array from cookie
if(isset($cart)){
    //Decode array from $cart variable and store it
    $cartArray = json_decode($cart);
    //Count number of items in $cart
    $cartAmount = count($cartArray);
    for($i = 0; $i < $cartAmount; $i++){
        //Check if $cart has the same product in it already
        if($productID == $cartArray[$i][0]){
            //Add to the amount of that product
            $cartArray[$i][1] += $productAmount;
            break;
        }
        //If it does not have the same product already, just add a new one
        else if($i == ($cartAmount - 1)){
            array_push($cartArray, array($productID, $productAmount));
            break;  
        };
    };
    //Recount number of items in $cart
    for($i = 0; $i < $cartAmount; $i++){
        $cartNumItems += $cartArray[$i][1];
    };
    //Encode $cart so it can be stored in cookie
    $cartRaw = json_encode($cartArray);
    //Create cookies
    setcookie('cart', $cartRaw, time() + (86400 * 7), '/');
    setcookie('cartNumItems', $cartNumItems, time() + (86400 * 7), '/');
    echo 'true';
}

更新:

我已经找到了重置cookie的东西,if语句中的这段代码:

//Decode array from $cart variable and store it
$cartArray = json_decode($cart);

当它运行时,$cartArray设置为 null,因此代码的其余部分正在处理 null 变量。现在我不明白的是为什么它被设置为 null。当我var_dump$cart变量时,我得到字符串(16)"[[''"21''",''"1''"]]"。但是当我跑步时...

echo json_decode($cart);

它只是空白的。

[['"21'",'"1'"]]不是

有效的JSON,因为字符'。这些不应该首先存在 - magic_quotes_gpc时代已经过去,除非你的PHP版本真的很旧。在对字符串调用json_decode之前删除它们。

因此,既然您说它不是在本地发生的,而是在您的网络托管商的服务器上发生的,那么这里很可能设置magic_quotes_gpc负责(如果您的 PHP 版本是 <= 5.3)。如果你的主机托管商没有提供任何方法来打开它(自定义php.ini,.htaccess),那么你可以使用get_magic_quotes_gpc()从脚本中检测它,然后使用f.e。 stripslashes删除它们。

(另请查看手册页上的用户注释以了解get_magic_quotes_gpc(),它们提供了通过几行脚本从所有外部输入数据中删除这些额外斜杠的解决方案。