Jump to content


Photo

Setting Session Variables From Within Class


  • Please log in to reply
5 replies to this topic

#1 jh_dempsey

jh_dempsey
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationLeeds

Posted 29 June 2006 - 12:33 PM

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:

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

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
$_SESSION[test_var] = "Test Success";

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

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 12:47 PM

try this:

foreach($row as $key => $value)
{
$_SESSION[$key] = $value;
}
Life's too short for arguing.

#3 jh_dempsey

jh_dempsey
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationLeeds

Posted 29 June 2006 - 01:04 PM

Sorry, that was a typo, thats what it is in my actual script

Ive edited my original post to correct this error

#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 01:35 PM

did you do session_start() before the foreach?


Life's too short for arguing.

#5 jh_dempsey

jh_dempsey
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationLeeds

Posted 29 June 2006 - 04:02 PM

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

#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 05:17 PM

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;
}
Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users