Home > DeveloperSection > Forums > get reference variable in php using session_decode result
john rob

Total Post:108

Points:756
Posted on    November-01-2014 12:04 AM

 PHP Session 
Ratings:


 1 Reply(s)
 646  View(s)
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'?


Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    November-01-2014 12:06 AM

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;

Don't want to miss updates? Please click the below button!

Follow MindStick