Help getting a session variable to pass though to other page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnitemare
Forum Newbie
Posts: 12
Joined: Sat Mar 28, 2009 1:14 pm

Help getting a session variable to pass though to other page

Post by phpnitemare »

Hi,

I have started a session on my index.php, and created a cart variable, to which when a user adds an item to a basket i will concatenate product id numbers so i can loop through them and display items in the basket.

the index code is as follows:

Code: Select all

session_start(); 
$_SESSION['cart'] = 30;

When trying to load the information from the variable into a temp $cart an error message is received:

Code: Select all

Notice: Undefined variable: _SESSION
The desired output would be 30, although it looks like the sessions are not passing through the pages, do i need to create a session id and how do i link this through to other pages to allow the variable data to remain constant throughout the session?

Code: Select all

$cart = $_SESSION['cart']; 
echo $cart;
Thanks for any help,
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help getting a session variable to pass though to other page

Post by Christopher »

Calling session_start() loads session data into the $_SESSION superglobal. You need to call it before you access session data. PHP does this so you are not required to incur the overhead of initializing sessions on pages that do not use session data.

Code: Select all

session_start();
$cart = $_SESSION['cart']; 
echo $cart;
(#10850)
Post Reply