Session Vars not staying

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
desius
Forum Newbie
Posts: 9
Joined: Thu Apr 17, 2008 1:38 pm

Session Vars not staying

Post by desius »

This is a very simple newb issue I'm struggling with... I'm trying to set a session variable and keep it on subsequent page accesses. So I use the following:

Code: Select all

 
session_start();
$_SESSION['myVar'] = $myData;
 
It's working fine on the first page visit, but anytime someone goes to the next page, the variable isn't there. Why? What is wrong here?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Session Vars not staying

Post by Christopher »

You must call session_start() on every page that uses $_SESSION. The function session_start() is what loads the data into the $_SESSION array.
(#10850)
desius
Forum Newbie
Posts: 9
Joined: Thu Apr 17, 2008 1:38 pm

Re: Session Vars not staying

Post by desius »

Sweet, thnx for the help, I'll give this a shot immediately. Will I need to register the session variable on the new page before using it or anything like that?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Session Vars not staying

Post by Christopher »

Yes. $_SESSION is empty until you call session_start().
(#10850)
Post Reply