I’m trying to store an integer value in session like this:
if(!isset($_SESSION['images'])){
if(!isset($_SESSION['user_id'])){
$_SESSION['images'] = array();
array_push($_SESSION['images'], (int)$newFilename );
}
} else {
if(!isset($_SESSION['user_id'])){
array_push($_SESSION['images'], (int)$newFilename);
file_put_contents("upload.txt", (int)$newFilename);
}
}
but the same code works on my localhost (php 5.3.13) but doesn’t work on the hosting with php 5.2.17 (this is the only difference I can see here).
I know that there are some problems with storing objects in $_SESSION, but i’m thrying to store there just a one-dimensional array like this:
$newFilename = $images->getLastFileId();
//$newFilename == array['200','201', '202'...n];
When $_SESSION[‘images’] has one element, all works correctly, but if there are >1 elements, I have an error &object(__PHP_Incomplete_Class)#1, and var_dump($_SESSION) shows that I have an object (looks like php tries to store in $newFilename an object $images):
array(1) { ["images"]=> &object(__PHP_Incomplete_Class)#1 (9) { ["__PHP_Incomplete_Class_Name"]=> string(15) "ImageCollection" ["imgSize"]=>
string(0) "" ["imgAngle"]=> string(0) "" ["imgUrl"]=> string(0) ""
["imgDir"]=> string(7) "upload/" ["thumbDir"]=> string(9)
"upload/m/" ["imgPreviewDir"]=> string(0) "" ["avatarsWidth"]=> int(50)
["avatarsDir"]=> string(8) "avatars/" } }
Again, this code works correctly on my localhost. Where could be the problem?
UPDATE I solved problem, but I’m not quite sure what causes this problem. Maybe it’s php bug, but my code starts to work correctly after I renamed $_SESSION['images'] into $_SESSION['imagesNew'] – so name of the $_SESSION variable is no longer the same as the name of the variable $images.
TL;DR – the code you’ve shown shouldn’t exhibit this behaviour, therefore it must be caused somewhere else.
For more information about the symptoms, imagine you have this class definition:
Let’s serialize that:
File contents (don’t try to copy/paste this, it won’t work):
Now, in another script, let’s
unserialize()that (without the class definition):Output:
Although your posted code doesn’t seem to suggest it, the output you’ve shown is that of an array of
ImageCollectionobjects, so for that to work you need torequirethe definition first, e.g.:Or use an
autoloader.