Jump to content


Photo
* * * * * 1 votes

dynamic session variables


  • Please log in to reply
4 replies to this topic

#1 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 23 May 2007 - 11:48 AM

ok this is a nice way tht saves u writing lines of
$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

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

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

$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

$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 :)

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 :P

er yer...luvlee

yeryer its simple i know, but hey, sum1 might find it helpful

Edited by curthard89, 23 May 2007 - 04:40 PM.


#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 May 2007 - 02:25 PM

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

Edited by rc69, 23 May 2007 - 02:27 PM.


#3 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 23 May 2007 - 02:29 PM

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

Edited by curthard89, 23 May 2007 - 02:58 PM.


#4 Mr. Jay

Mr. Jay

    Young Padawan

  • Members
  • Pip
  • 81 posts

Posted 15 June 2007 - 02:01 AM

Wouldn't this be secure & possible to do?

if (('id' == [mysql user id]) && ('pass' == [mysql user password])) {
continue;
}

#5 mandrill

mandrill

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 19 January 2009 - 02:31 PM

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()?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users