管理Mulipte用户的购物车会话

时间:2018-08-09 07:31:14

标签: php jsp

我创建了购物车名称$ _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;

    }


}

1 个答案:

答案 0 :(得分:0)

这是由于未使用'session_start()'来初始化唯一会话的结果。只需在使用会话之前添加它,就可以了。