Re: -forgot- Storing objects in a session PHP 5.2.0



"Anthony Smith" <mrsmithq@xxxxxxxxxxx> wrote in message
news:1164640923.876032.181280@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have read the message boards in this group and others. I still have
not been able to pull my obect out of a session and use it. Here is how
I store it in a session:

<?
//Include the UserClass & RoleClass so that we can create a user
object.
require_once("UserClass.php");
require_once("RoleClass.php");

//This has to be the first line of code a file if sessions are used.
session_start();

//Create a new user object
$user = new User();
$user->setName($_SERVER['HTTP_OBLIX_GIVENNAME']);

$role = new Role();
$role->setSysName($xml->roles[$i]->systemName);
//Now add the role to the user object.
$user->addRole($role);

//Save the user object in the session
$_SESSION['user'] = $user;

Not like this...

?>

This is how I try to pull the object out:

Wrong again. You can't store objects into a session per se, you need to
serialize and unserialize them in order to use them in a session. Here's
something you should read:
http://php.net/manual/en/language.oop.serialization.php

When you store the object to the session, first serialize it. Then, when
retrieving it, you unserialize it.

page 1:
$_SESSION['user'] = serialize($user);

page 2:
$user = unserialize($_SESSION['user']);

And remember to always include the class definition on all pages where you
use the class.

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
spam@xxxxxxxxxxxxx | rot13(xvzzb@xxxxxxxxxxxxx)


.


Quantcast