jh_dempsey Posted June 29, 2006 Share Posted June 29, 2006 Hey AllIm having a real headscratch here when trying to set session variables from inside a classI 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 Quote Link to comment https://forums.phpfreaks.com/topic/13201-setting-session-variables-from-within-class/ Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 try this:foreach($row as $key => $value){$_SESSION[$key] = $value;} Quote Link to comment https://forums.phpfreaks.com/topic/13201-setting-session-variables-from-within-class/#findComment-50801 Share on other sites More sharing options...
jh_dempsey Posted June 29, 2006 Author Share Posted June 29, 2006 Sorry, that was a typo, thats what it is in my actual scriptIve edited my original post to correct this error Quote Link to comment https://forums.phpfreaks.com/topic/13201-setting-session-variables-from-within-class/#findComment-50807 Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 did you do session_start() before the foreach? Quote Link to comment https://forums.phpfreaks.com/topic/13201-setting-session-variables-from-within-class/#findComment-50815 Share on other sites More sharing options...
jh_dempsey Posted June 29, 2006 Author Share Posted June 29, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/13201-setting-session-variables-from-within-class/#findComment-50882 Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 try a debug code and see what have printed out.foreach($row as $key => $value){//debugecho "key: $key, values: $values<br>";//end debug$_SESSION[$key] = $value;} Quote Link to comment https://forums.phpfreaks.com/topic/13201-setting-session-variables-from-within-class/#findComment-50916 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.