[SOLVED]More $_SESSION object woes...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

[SOLVED]More $_SESSION object woes...

Post 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!
Last edited by evilmonkey on Sat Aug 14, 2004 6:20 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;))
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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?
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Hmm, I see. Well, thank, I hope that'll take care of my problems.
Post Reply