Jump to content

Problem with $_SESSION[$var]


tmyonline

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/105755-problem-with-_sessionvar/
Share on other sites

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.

 

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.

Archived

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

×
×
  • Create New...

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.