Re: Serializing objects and storing them is sessions [fixed]
| From: | Kelly Hallman | Date: | Fri, 09 Apr 2004 15:05:59 +0000 |
| Subject: | Re: Serializing objects and storing them is sessions [fixed] | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182988@lists.php.net to get a copy of this message | ||
Apr 9 at 1:44am, Jason Giangrande wrote:
> Jason Giangrande wrote:
> > I'm having a problem unserializing objects that are passed from page to
> > page with sessions. Registered globals is disabled so I am using the
> > $_SESSION array to store session variable
> >
> > When I var_dump() $_SESSION it appears to be a string representation of
> > the object. However, when I try to unserialize() it and store it in
> > $auth the value is bool(false) and not an object.
> >
> > Can anyone tell me what I am doing wrong?
>
> Don't I feel stupid now. I have solved my problem. I had written a
> __sleep() function that I had forgotten about, and as it turns out, it
> was breaking things. Not really sure why. It was just creating the
> array of object properties to be serialized. Commenting it out fixes
> the problem I was having.
You shouldn't serialize() objects prior to assign to a session variable.
The default session handler automatically serializes the data. Assigning a
serialized object value to a session just adds redundancy and overhead.
The only caveat: the class must be defined prior to session_start(), for
the same reason a class needs to be defined prior to unserializing.
http://www.php.net/manual/en/language.oop.serialization.php
--Kelly