tmyonline Posted May 15, 2008 Share Posted May 15, 2008 Hi guys: I'm wondering if the $_SESSION superglobal array can contain a variable. I experience that $_SESSION['string'] works but $_SESSION[$variable] does not. In my program, I need the $_SESSION to store a variable. If I use $_SESSION['string'], it only remembers the last value when it goes through the loop. Any ideas ? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/ Share on other sites More sharing options...
StormTheGates Posted May 15, 2008 Share Posted May 15, 2008 Have you tried the session with ""s? $_SESSION["$variable"]; Not sure if it will make a difference. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541887 Share on other sites More sharing options...
tmyonline Posted May 15, 2008 Author Share Posted May 15, 2008 Yeah, I did but it didn't make any difference! Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541894 Share on other sites More sharing options...
DarkWater Posted May 15, 2008 Share Posted May 15, 2008 What do you mean? Please elaborate. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541911 Share on other sites More sharing options...
tmyonline Posted May 15, 2008 Author Share Posted May 15, 2008 In my program, I used $_SESSION[$variable], it does not work; it does not remember the value I set it equal to. So, I'm wondering whether $_SESSION can contain a variable or only string. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541917 Share on other sites More sharing options...
DarkWater Posted May 15, 2008 Share Posted May 15, 2008 Uhh, you seem to not understand what an array is. $test = "Ha, this is a test!"; $_SESSION['test'] = $test; echo $_SESSION['test']; //Outputs: Ha, this is a test! Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541919 Share on other sites More sharing options...
tmyonline Posted May 15, 2008 Author Share Posted May 15, 2008 Uhh, you seem to not understand what an array is. Please re-read my question again. I already know $_SESSION['string'] works. Your $_SESSION['test'] is an example that $_SESSION can contain a string. My question is whether $_SESSION can contain a variable, i.e., whether $_SESSION[$variable] works because I used it in a loop like this: while (...) { ... $_SESSION[$variable] = $variable; } So, my $_SESSION will be an associative array whose key values are different strings. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541930 Share on other sites More sharing options...
DarkWater Posted May 15, 2008 Share Posted May 15, 2008 Yes, it works. Don't use ridiculous keys though, I think they have a maximum length. $key = "test"; $_SESSION[$key] = "Lololol". echo $_SESSION["test"]; //Outputs: Lololol. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-541940 Share on other sites More sharing options...
tmyonline Posted May 15, 2008 Author Share Posted May 15, 2008 Yes, it works. Don't use ridiculous keys though, I think they have a maximum length. $key = "test"; $_SESSION[$key] = "Lololol". echo $_SESSION["test"]; //Outputs: Lololol. Have you tested your code at all ? It's not working. It seems that a variable cannot be used as an index of the $_SESSION array but you say yes without even testing it. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-542007 Share on other sites More sharing options...
trq Posted May 15, 2008 Share Posted May 15, 2008 It does work. Are you sure you have a call to session_start() prior to using the $_SESSION array? Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-542014 Share on other sites More sharing options...
tmyonline Posted May 15, 2008 Author Share Posted May 15, 2008 Yes, I do. Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-542018 Share on other sites More sharing options...
kenrbnsn Posted May 15, 2008 Share Posted May 15, 2008 Can you post the code that's not working? Ken Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-542022 Share on other sites More sharing options...
wildteen88 Posted May 15, 2008 Share Posted May 15, 2008 Post the code for your loop. Variables can be used to reference array keys, as suggest by DarkWater's perfectly valid code. Doing $key = "test"; $_SESSION[$key] = "Lololol" is the same as $_SESSION['test'] = "Lololol" Quote Link to comment https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/#findComment-542029 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.