Re: Issue with the casting of a SESSION variable
- From: Rzzap <rzzap@xxxxxxxxxxxxxxxx>
- Date: Wed, 29 Oct 2008 15:58:32 +0100
AndreH wrote:
Good day,
I have the a bit of an issue with retrieving an object from php's
session.
I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);
And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);
Why? A session already serializes its values when stored, no need to do it twice. You can perfectly store an object in a session, as long as you've loaded the class definitions before session_start().
Keep in mind not every object can be serialized, see the manual:
http://nl2.php.net/serialize
"serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serialize()ing will also be stored. Any other reference will be lost."
Something not serializable is by default not storable in a session, allthough you could employ the __sleep() & __wakeup() methods to remove any unserializable content, and recreate them on unserialize.
--
Rik
.
- References:
- Issue with the casting of a SESSION variable
- From: AndreH
- Issue with the casting of a SESSION variable
- Prev by Date: Re: Class variable interpolation
- Next by Date: IE6 strange behaviour with fwrite calls
- Previous by thread: Re: Issue with the casting of a SESSION variable
- Next by thread: IE6 strange behaviour with fwrite calls
- Index(es):
Relevant Pages
|