1

I'm building a shopping cart for a school assignment. I'm pretty much there except for adding the items to the cart variable.

I've set up my cart session with: $_SESSION['temp_session'] = array();

and then, when a item is added to the cart, this code is executed

if (isset($_POST['addtocart'])) {
            $item_name = $_POST['item_name'];
            $price = $_POST['price'];
            $qty = $_POST['qty'];

            $newItem = $item_name.":".$price.":".$qty;
            //echo $newItem;

            if (isset($_SESSION['shop_session'])) {
                array_push($shop_session, $newItem);
                //header('Location: cart.php');
                print_r($shop_session);
            }
            else {
                array_push($temp_session, $newItem);
                //header('Location: login.php?notLoggedIn=true');
                print_r($temp_session);
            }
        }

it all seem s to be working fine (i can print out $newItem and it contains the elements) but when I try to add $newItem to either $shop_session or $temp_session and then print them out, there array is empty.

Is this something to do with the way I'm using array_push() ?

Thanks

2 Answers 2

4

Is your RegisterGlobals is on? If not you must use

            if (isset($_SESSION['shop_session'])) {
                    array_push($_SESSION['shop_session'], $newItem);
                    //header('Location: cart.php');
                    print_r($_SESSION);
            }
            else {
                array_push($_SESSION['temp_session'], $newItem);
                    //header('Location: login.php?notLoggedIn=true');
                    print_r($_SESSION);
            }
Sign up to request clarification or add additional context in comments.

3 Comments

@Anax If you don't know how that relates to register_globals then you've never come across a script that has been coded with it on.
Ah, ok, thanks guys. with the print_r($_SESSION); i'm getting: Array ( [temp_session] => Array ( [0] => Apocalypso:10.00:2 ) ). So $_SESSION is an array, and within it is another array called temp_session? how would I copy temp_sessions to shop_session? and also, I guess a foreach($_SESSION['shop_session'] as $item) isn't going to work either?
@e.c.ho My initial impression was that Alexey was implying that with register_globals On, every session variable is automatically available as standalone variable as well, which I believe is not true.
3

You can't type

$_SESSION['temp_session'] = array();

and expect to have a variable named $temp_session. All you have is an array ($_SESSION) which contains an element (with key name temp_session), which in turn is an array as well.

In order to have the desired results you need an additional line, such as:

$temp_session = $_SESSION['temp_session'];

and when you update that, back again into your session:

$_SESSION['temp_session'] = $temp_session;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.