简体   繁体   中英

First item added to $_SESSION array is added TWICE…why?

Yesterday I posted a question on SO HERE regarding a multidimensional $_SESSION array I was working with to keep track of items in a shopping cart I'm developing. One of the very helpful responses suggested I create the $_SESSION array not as a multidimensional array, which I agree is a smoother approach.

The $_SESSION array is started when a user clicks any of the 'Add to Cart' buttons on the store page. My original code with the multidimensional array is this:

$quantity = 1;

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
    $_SESSION['cart'] = array();
    $_SESSION['cart'][$_POST['product_description']] = array('quantity' => $quantity, 'price' => $_POST['product_price']);
    header("Location: http://website.com/cart.php");
    }

And this worked fine, but I was not able to achieve the results with this that I wanted (see the other post).

The code I was suggested to use is this:

$quantity = 1;

// add first item from shop page
if(isset($_POST['add_item']) && (!isset($_SESSION['cart']))) {
$_SESSION['cart'][] = array('product_description'=> $_POST['product_description'], 'quantity' => $quantity, 'price' => $_POST['product_price']);
header("Location: http://website.com/cart.php");
}

And it works great and allows me to do what I want to do (again, see the other post for details).

HOWEVER...

Regardless of which 'Add to Cart' button is clicked on the store page (there are only four), the FIRST click gets information added to the $_SESSION['cart'] array TWICE. Every other button clicked afterward is entered only once. And again, it does not matter which button is clicked first, this happens no matter what.

Here's the result of a print_r on the new $_SESSION array code:

Array
(
    [0] => Array
        (
            [product_description] => iPhone case - Black
            [quantity] => 1
            [price] => 9.49
        )

    [1] => Array
        (
        [product_description] => iPhone case - Black
        [quantity] => 1
        [price] => 9.49
    )

)

I have scoured SO and Google for clues as to why this is happening, but no matter how I word my queries, I just can't seem to find anything that helps.

It has to be something simple that I am missing...

Sincere thanks in advance!

UPDATE

To clarify, there are only 4 items on the store page. There won't ever be any more. To keep things super simple, each item has its own 'Add to Cart' button. When the button is clicked, the user is taken to the cart page where they can adjust the quantity or delete the item then either check out cancel and clear the cart or continue shopping.

Your description seems to say you want something like:

  • check to see the cart exists, create if it doesn't

  • then add the item to the cart

The code you posted seems to only add an item, if you don't have a cart yet.

The only reason I can think of for the double item though is that there is more code than you posted and you have the code for the "add item" bit happening in both the "create cart" and a later "add item" chunk.

Here's what I would do:

quantity = 1;

// ensure cart
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}

// add item from shop page
if(isset($_POST['add_item'])) {
    $_SESSION['cart'][$_POST['product_description']] = array(
        'quantity' => $quantity, 
        'price' => $_POST['product_price']
    );

    // or if you prefer the numeric indexed array:
    // $_SESSION['cart'][] = array(
    //     'product_description'=> $_POST['product_description'], 
    //     'quantity' => $quantity, 
    //     'price' => $_POST['product_price']);

    header("Location: http://website.com/cart.php");
}

I've modified your code in place to keep the logic simple, but like @dognose comments: do not trust the prices passed via the post. It is far to simple to hack a form and post you bad data to get your stuff for $0.01 like that.

Cannot answer as the full code is not provided; However:

$_SESSION = array_map("unserialize", array_unique(array_map("serialize", $_SESSION)));

will remove the duplicates from your session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM