Page 1 of 1
[SOLVED]More $_SESSION object woes...
Posted: Sat Aug 14, 2004 12:38 pm
by evilmonkey
Hello. I am storing a serealized object in the $_SESSION[] autoglobal. I serialize it in order to avoid it bieng an incomplete function (whatever that means, that problem, as I have seen, is very poorply documented). After the user logs in, I create a new instance of the User class that holds all the information about the given user as well as few generic functions:
Code: Select all
<?php
ob_start();
//check login, etc
session_start();
$user = new User;
$_SESSION['user'] = serialize($user);
//more stuff
ob_end_flush();
?>
This is how I read the contennts of the $_SESSION['user'] object:
Code: Select all
<?php
session_start();
$user = unserialize($_SESSION['user']);
echo $user->username;
echo $user->email;
//etc..
?>
The problem is, I can only run that script once. If I go to another script and come back, or if I refresh the page, I will not see the values of the attributes of the $user object. The only way for me to see them again is to re-login. Can someone please tell me why that happens and what I can do to fix it?
Thanks!
Posted: Sat Aug 14, 2004 1:07 pm
by markl999
Do you have register_globals On or Off.
I'd also go back to trying to work out why you need to serialize the object before storing it in the session, you shouldn't have to. All data in the $_SESSION superglobal is automatically serialized and i've never had a problem before storing objects in sessions. (i think i remember your previous post on the $_SESSION/serializing problem so i'll go and find that

)
Posted: Sat Aug 14, 2004 2:54 pm
by evilmonkey
Hello Mark.
My register globals is on, but I am avoiding coding in a way that would rely on them. PHP on my server was just recently reinstalled (umm..yesterday?), but it still requires serialization in order to function.
Any suggestions?
Posted: Sat Aug 14, 2004 4:01 pm
by markl999
register_globals being On could easily be the problem as doing anything with $user will also affect the session registered $user. The only way to be sure though would be to turn register_globals Off if possible and test storing the user object in the session 'normally' (i.e without serializing) as it is stored serialized automatically.
Failing that then it must be down to the structure of the user class. Is it a simple class or does it instantiate other classes inside of it etc?
Posted: Sat Aug 14, 2004 4:35 pm
by evilmonkey
Disabling register_globals seems to have solved this problem (let's hope it didn't create a ton of new problems). Do you think the serializing should not be necesary now?
Posted: Sat Aug 14, 2004 4:37 pm
by markl999
serializing shouldn't have been necessary as all session data is serialized anyway. Serializing the user object before it was automatically serialized just 'hid' the error but wasn't really fixing it.
Posted: Sat Aug 14, 2004 6:19 pm
by evilmonkey
Hmm, I see. Well, thank, I hope that'll take care of my problems.