note 113315 deleted from session.examples.basic by cmb
| From: | cmb@php.net | Date: | Wed, 07 Dec 2016 16:00:52 +0000 |
| Subject: | note 113315 deleted from session.examples.basic by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208264@lists.php.net to get a copy of this message | ||
Note Submitter: guy at syntheticwebapps dot com
----
Despite the warning about not being able to use references inside the session space, I've done
it in the past and apparently completely successfully. That is, I can do something like this:
<?php
session_start();
if (!$_SESSION['favorite']) {
$_SESSION['cow'] = "Elsie";
$_SESSION['favorite'] =& $_SESSION['cow'];
echo "We set cow = '$_SESSION[cow]' and favorite =& cow
($_SESSION[favorite]).<br/>Reload the page to see if both change when one changes
later.<br/>";
} else {
echo "Having re-entered the session after initial settings were made: cow = $_SESSION[cow] and
favorite = $_SESSION[favorite].<br/>";
$_SESSION['cow'] = "Bessie";
echo "We reassigned cow = $_SESSION[cow] and our restored reference variable favorite =
$_SESSION[favorite]<br/>Note the presence of the &s in the var_dump
below.<pre>";
var_dump($_SESSION);
echo "</pre><br/>If you reload, the test will begin again.";
unset($_SESSION['cow'], $_SESSION['favorite']);
session_destroy();
}
?>
yields the following after the second request:
Having re-entered the session after initial settings were made: cow = Elsie and favorite = Elsie.
We reassigned cow = Bessie and our restored reference variable favorite = Bessie
Note the presence of the &s in the var_dump below.
array(2) {
["cow"]=>
&string(6) "Bessie"
["favorite"]=>
&string(6) "Bessie"
}
If you reload, the test will begin again.
I've found this ability very useful and storage efficient in the session data.