In some third party code, that I am not allowed to change, something peculiar is happening.
They write a variable (an array containing objects) to the session (without serialising it) and then iterate using a foreach on the original variable (without using references). Whenever they change a value, the corresponding value in the session is also changed. I was able to create a smaller example that has the same behaviour:
$test = array((object)array("categories" => "test"));
$_SESSION['woot'] = $test;
print_r($_SESSION['woot']);
foreach ($test as $a) {
if (!is_array($a->categories)) $a->categories = array();
}
print_r($_SESSION['woot']);
This is the result:
Array
(
[0] => stdClass Object
(
[categories] => test
)
)
Array
(
[0] => stdClass Object
(
[categories] => Array
(
)
)
)
I already noticed that, when I serialize and unserialize the object array, the problem does not occur.
Does anyone have an idea about what's happening here? Is it the code? Is it an incorrect server setting? I'd like to know a little more before I contact the devs of the code.
Additional information:
- I am using PHP Version 5.3.14
- Register globals is switched off
Regards, Joost.