Help - Search - Members - Calendar
Full Version: dynamic session variables
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
curthard89
ok this is a nice way tht saves u writing lines of
CODE
$name = $_SESSION['name'];


this will work if your $x variable is same as $_SESSION['x'] so $x = $_SESSION[x];

in other words, only works if the two x values r the same.

so say we had

CODE
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$message = $_SESSION['message'];


what we can do to help ourselved typing those out all the time is type

CODE
$session = $_SESSION;

foreach($session as $key => $value){
    $$key = $_SESSION[$key];
}


if there is a session variable you dont want to include in the loop such as a user logged in variable

u can do sumthing simple like

CODE
$session = $_SESSION;

foreach($session as $key => $value){
   if($key == 'userloggedin'){
     continue;
   }
    $$key = $_SESSION[$key];
}


userlogged in can be changed for anything you want, any variable or string smile.gif

the $key is the session name and $value is what content is stored within the session name

they can be changed to anything u like
so $key cud b $cheese and $value cud b $ontoast

lol

$$key will generate the variable

so all that does, it runs through the session

pulling out the name and placing name

$here = $_SESSION[$here];

there fore generating all our session variables smile.gif

er yer...luvlee

yeryer its simple i know, but hey, sum1 might find it helpful
rc69
CODE
   if($key == $userloggedin){
     continue;
   }

You have to keep in mind, doing that compares the values of the variables. So, if $userloggedin is defined as true, which by the sounds of the name would be a possible value, it will end up evaluating something like:
CODE
   if('userloggedin' == true){
     continue;
   }

Which would result in a false return resulting in continue not being called. What you would want to do is use a string rather than a variable (or a variable that contains the name of the key you want to skip):
CODE
   if($key == 'userloggedin'){
     continue;
   }


More info about the key feature of this tutorial can be found at php.nets explaination of variable variables. If you scroll down to the user comments, there is also a ton of little scripts that employ a similar idea but with a different super-global.

p.s. I don't want to hijack your tutorial or completely disprove it or anything, i'm just sure somebody will complain about that variable thing (or something else), so i'm beating them to the punch.
curthard89
lol, i wrote it cuz i had to do huge form at work and cudnt b arsed to right like 200 lines of $_SESSION[''] =

lol, so i came up with that and reading back, i didnt mean to put it as a variable lol sory
Mr. Jay
Wouldn't this be secure & possible to do?

if (('id' == [mysql user id]) && ('pass' == [mysql user password])) {
continue;
}
mandrill
so if I have a dynamically named variable such as $a_{$b} (the variable name being $a_thevalueofb)
I can store that in a $_SESSION using the above method?

Some further explanation please, do i not have to use session_register()?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.