Page 1 of 1

Variable Variable named sessions

Posted: Mon May 01, 2006 5:12 pm
by bigtaf
I need to be able to creat variable variable named sessions. So basically

$curdev = "device".$_SESSION['HTinfo']->numdevicesused;

ends up being "device1", "device2", etc. If i echo it, it displays the correct name

then I assign information to it with

$_SESSION[$curdev]->thing1=99; (ive also tried ($_SESSION[$$curdev]->thing1=99)

so this should in theory create $_SESSION[device1]->thing1=99

however when I try and access it as such

echo ($_SESSION['device1']->thing1);

or

echo ($_SESSION["device1"]->thing1);

or

echo ($_SESSION[device1]->thing1);

I get nothing. Ive been using session variables throughout this project and all other variables work fine. Can you not assign variable variable names for sessions? or any suggestions as to why this may not work

Posted: Mon May 01, 2006 5:25 pm
by Christopher
You are serializing an object into the session. There are some special rules regarding that -- mainly that the class has to be loaded before you call session_start(). Maybe just use multi-dimensional arrays? (e.g. $_SESSION[$curdev]['thing1'] = 99; )

Posted: Mon May 01, 2006 5:32 pm
by bigtaf
can I just unserialize it?

I think arrays could make this alot more complicated in my case.

is I include the object info

then start the session

then call the object

then try and get the info

Posted: Mon May 01, 2006 6:18 pm
by Christopher
If you inlcude the class before you call session_start() then the session handler will automatically unserialize it for you. Likewise it will serialize it (like all the other vars in the session).