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
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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.