GET REFERENCE VARIABLE IN PHP USING SESSION_DECODE RESULT

john rob

Total Post:108

Points:756
Posted by  john rob
PHP
 949  View(s)
Ratings:
Rate this:
I noticed that the PHP session_decode() function has an object type for PHP reference variables signified by 'R'. However, I can't seem to find out how to get session_decode() to output this object type. If I assign a reference variable to $_SESSION, and then call session_decode(), the reference variable is represented by session_decode() as having the same object type as the variable it references and not 'R'. What is the use case for the session_decode object type of 'R'?
  1. Jeet Verma

    Post:110

    Points:776
    Re: get reference variable in php using session_decode result

    For session_encode to encode a variable as a reference, the referenced variable needs to be in session too. If it's not in session, then it will encode it as the original type.

    $foo = 'bar';
    $_SESSION['foo'] = $foo;
    $_SESSION['bar'] = &$_SESSION['foo'];
    $_SESSION['baz'] = &$foo;
    $_SESSION['hey'] = &$foo;
    var_dump(session_encode());

    Outputs:

    foo|s:1:"bar";bar|R:2;baz|s:3:"bar";hey|R:4;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!