Make $ _SESSION work as session_register

0

I changed my session_register to $_SESSION , however $_SESSION does not allow me to use the variable in the same way as session_register .

For being deprecated I thought they corresponded the same thing. However the use after the declaration is very different, session variables generated by the session_register can be used normally, deplacaras by $ _SESSION not.

$nome = "Haxz";
session_register("nome");

After the declaration, simply using $nome it already returns the value. As a common variable.

$_SESSION["nome"] = "Haxz";

It does not allow me to use the variable as $nome , only as $_SESSION["nome"] .

What intrigues me is that both can be tested in the same way isset($_SESSION["nome"]) and in print_r($_SESSION) , are shown equal.

I do not want to change my entire project (it's pretty big) How do $_SESSION["nome"] respond equal to session_register("nome") ? (respond in the sense that you can work with it only with a $nome matching variable.)

    
asked by anonymous 13.11.2014 / 13:44

1 answer

3

Do not do this.

Simply do this at the end of the code:

    $_SESSION['nome'] = $nome;

And in the beginning recover with

    $nome = $_SESSION['nome'];


... But if you really want to do, a solution would be this:

function meu_session_register($nome){
    global $$nome;
    $_SESSION[$nome] = $$nome;
    $$nome = &$_SESSION[$nome]; 
}

Explanation:

  • First, we declare a global variable with the same $nome of the variable;

  • Then, we save in the session the

    value
  • Then, we reassign the value to the variable passing by reference ( & ), so that subsequent changes change the session value.

13.11.2014 / 18:08