Jump to content

Archived

This topic is now archived and is closed to further replies.

jh_dempsey

Setting Session Variables From Within Class

Recommended Posts

Hey All

Im having a real headscratch here when trying to set session variables from inside a class

I have a page called uni_login.php which includes the file with my class in, and then creates a new instance of the class.

One of the functions in the class is the do_login function, and this function check for correct login details, and then will grab all the info from the database and attempt to store this info inside a session so i can get to it later.
Im doing it like this:

[code]foreach($row as $key => $value)
{
$_SESSION[$key] = $value;
}[/code]

All seems to work fine because if i do a var_dump($_SESSION) on my uni_login.php page after i have called the do_login() function then it all seems to display fine. All the values are there along with the correct array keys.

Now...

If i go to another page (index.php), i start a session with session_start(); and attempt to output some of the variables from within my session. Nothing happens. They are all completley blank. Doing a var_dump($_SESSION) on the index.php page shows it to be completely empty.

Ive checked that the session id being used is the same on both pages, and it is, so thats not the problem.

Next i tried just typing this somewhere on my uni_login.php page
[code]$_SESSION[test_var] = "Test Success";[/code]

Now if i go to my index.php page and try and print this variable, it works fine.

So what am i doing wrong?? Is there something special i need to do to store session variables from within a class??

Thanks

Share this post


Link to post
Share on other sites
try this:

foreach($row as $key => $value)
{
$_SESSION[$key] = $value;
}

Share this post


Link to post
Share on other sites
Sorry, that was a typo, thats what it is in my actual script

Ive edited my original post to correct this error

Share this post


Link to post
Share on other sites
did you do session_start() before the foreach?

Share this post


Link to post
Share on other sites
Yes, session_start() has been called at the top of each page the sessions appear on. Ive even tried putting a session_start(); inside the constructor for the login class

Share this post


Link to post
Share on other sites
try a debug code and see what have printed out.

foreach($row as $key => $value)
{
//debug
echo "key: $key, values: $values<br>";
//end debug

$_SESSION[$key] = $value;
}

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.