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

7
  • 2
    Please elaborate why you can't just use an ordinary $_SESSION["$key"] array access. Commented Oct 14, 2011 at 6:25
  • why you want to use variable variables? Commented Oct 14, 2011 at 6: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 Commented Oct 14, 2011 at 7:02
  • @Mala have you tried the solution I posted earlier in my edited answer? It should work like you expected Commented Oct 14, 2011 at 7: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. Commented Oct 14, 2011 at 7:14

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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.
With respect to the code you provided, unless I'm mistaken, that's reading the variables, not writing...
1

Isn't this way better?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.