0

I am trying to implement a shopping cart object for the user, storing a serialized version of the cart in the $_SESSION super global at the index 'user_cart', however I am running into an issue where the data I have stored at that index no longer exists when I move to a new page.

I do have session_start at the top of every page, and when I echo session_id(), I do get the same session id across multiple pages. In addition, when I echo $_SESSION on both pages, the page the object gets created on displays correctly as follows:

Array ( [user_cart] => Array ( [count] => 0 [items] => Array ( ) ) )

However, once I actually click on add to cart to order something, then navigate to my 'view cart' page, I get the following as my $_SESSION variable:

Array ( [user_cart] => )

Where it recognizes that my $_SESSION array has an index 'user_cart' however nothing is stored inside of it.

This ONLY happens if I add something to cart first, if I were to just go to the view cart page without adding anything to the cart, I get the same array printed as I did on the homepage where the session is created.

Here is some of the code I have in my 'add_to_cart_handler.php' file where I think the issue is arises:

$serialized=$_SESSION['user_cart'];
$cartobj=new ShoppingCart();
$cartobj->__unserialize($serialized);
$cartobj->addToCart($new_item_id, 1);

$_SESSION['user_cart']=$cartobj->__serialize();
header('Location: http://localhost/website/heroku/order.php');

Here is a snippet of both my __serialize() and __unserialize():

    public function __serialize() {
        return [
            'count'=>$this->count,
            'items'=>$this->serialize_items()
        ];
    }
    
    public function __unserialize(array $data) {
        $this->count=$data[0];
        $this->items=$this->unserialize_items($data[1]);
    }

    public function serialize_items() {
        $serialized=array();
        foreach($this->items as $item) {
            $serialized[]=$item->__serialize();
        }
        return $serialized;
    }

    public function unserialize_items() {
        $unserialized=array();
        foreach($unserialized as $item){
            $tmpItem=new CartItem();
            $tmpItem->unserialize($item);
            $unserialized[]=$tmpItem;
        }
    }

Where unserialize_items() and serialize_items() are essentially the same thing, but with the CartItem object.

EDIT:

It might be worth noting that my $_SESSION['user_cart'] has a value of null.

backward forward
  • 429
  • 3
  • 17
  • An alternative method is to use array_push (to add an element to an array) for adding an item to the shopping cart. – Ken Lee Nov 08 '20 at 04:10
  • @KenLee right, but nonetheless what ever is pushed to the array is not persistent across pages, and the index is held, with null data – backward forward Nov 08 '20 at 04:13
  • One question - apart from the item, do you need to store the "quantity" for each item in the shopping cart ? – Ken Lee Nov 08 '20 at 04:16
  • @KenLee Ideally. The quantity for each item isn't actually stored in the shopping cart directly, but rather in each CartItem, so when the user views their cart after multiple "add to cart" submissions, the cost is reflected by multiplying item cost and the quantity, and also so the company owners will know how many of each item is requested. – backward forward Nov 08 '20 at 04:30
  • @KenLee but if a solution insists on no quantity, then I will take it lol – backward forward Nov 08 '20 at 04:31
  • 1
    your `unserialize_items` method doesn't return anything – lagbox Nov 08 '20 at 04:47
  • solution below. – backward forward Nov 08 '20 at 05:38
  • Nice to note that you have solved the problem yourself. Next time please add quantity to the cart. You also need to handle the case when a user enters the same item into the shopping cart (either ignore this action or increment the quantity by 1). On the other hand, sometimes in certain trade an item may have different colors and size, usually you will also need to handle such variations in your cart (just to share my experience with you) – Ken Lee Nov 08 '20 at 17:19

1 Answers1

-1

The solution can be found here: PHP session lost after redirect

The issue was that the script was still running in the addtocart_handler, and when redirected back to the most previous page, the script was still running.

backward forward
  • 429
  • 3
  • 17
  • 1
    Links to answers should not be posted as answers. If the linked answer truly answered your question, delete yours as a duplicate – Wesley Smith Nov 08 '20 at 07:02