2

I would like to set a session variable with something akin to:

$key = '_SESSION[element]';
$$key = 'value';

This does indeed set $_SESSION['element'] equal to value, but it also seems to clear the rest of my $_SESSION variable, resulting in the $_SESSION array only containing the new key/value pair.

How can I write into the session using variable variables without nuking it?

Edit: if this can't be done, so be it, we'll probably have to restructure and do things the "right" way. I just wanted to know if there was an easy fix

Mala
  • 14,178
  • 25
  • 88
  • 119
  • 2
    Please elaborate why you can't just use an ordinary `$_SESSION["$key"]` array access. – mario Oct 14 '11 at 06:25
  • why you want to use variable variables? – Gianpaolo Di Nino Oct 14 '11 at 06:36
  • @mario: this piece of code handles a lot of (non-session) variable assignments, and I can't edit it (without approval, etc) - my module only has control over what keys and values it sends in. If this can't be done we'll restructure and do it "right" but if there was an easy fix to get this done from my module alone it would be lovely – Mala Oct 14 '11 at 07:02
  • @Mala have you tried the solution I posted earlier in my edited answer? It should work like you expected – Kemal Fadillah Oct 14 '11 at 07:08
  • 2
    To answer your question: No, variable variables can only reference another variable base name. They are not variable expressions by themselves. It's an edge case that your assignment succeeded oddly. – mario Oct 14 '11 at 07:14
  • As @mario says it is very strange that the assignment succeeded. I couldn't replicate it. The line `$$key = 'value';` actually creates a variable with the name `_SESSION[element]` which is not part of the SESSION array. As proposed by @RajanRawal `eval` does the trick. – Martin Dimitrov Oct 14 '11 at 07:44
  • @Kermal I couldn't as I can't edit the assignment part. In any case, thanks everyone for the insight – Mala Oct 14 '11 at 09:41

3 Answers3

2

From PHP Documentation:

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

How you ended up with a situation like this, is really questionable. You're probably doing something wrong.

EDIT

This little trick should give you what you want:

$key = '_SESSION[element]';
$key = str_replace(array('_SESSION[', ']'), '', $key);
$_SESSION[$key] = 'value';
var_dump($_SESSION);

This will basically produce the same results as xdazz's answer

Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
  • Regarding the quote from the docs, yeah I saw that, but I'm not in a function or method in this part of the code. I know doing it this way is "wrong" and it'd be better to make this not necessary, but if a 5-minute fix will save a 50-hour restructure in a block of throwaway code, i'm all for it. – Mala Oct 14 '11 at 06:32
  • With respect to the code you provided, unless I'm mistaken, that's reading the variables, not writing... – Mala Oct 14 '11 at 06:34
2

@Mala, I think eval will help you. Check the code below. It may help you for what you want.

session_start();
    $_SESSION['user1'] = "User 1";
    $_SESSION['user2'] = "User 2";

    $key = "_SESSION['user3']";
    eval("\$$key = 'User 3';");

    foreach ($_SESSION as $key=>$value){
        echo $key." => ".$value."<br/>";
        unset($_SESSION[$key]);
    }
    session_destroy();

If you still have any trouble, Let me know. Thank you

Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62
1

Isn't this way better?

$key = 'element';
$_SESSION[$key] = 'value';
xdazz
  • 158,678
  • 38
  • 247
  • 274