Web Hosting Talk







View Full Version : Objects Not Being Saved In Sessions


stodge
05-30-2003, 11:06 AM
This is a weird one; I've spent almost an hour going over each line of code several times, but I still don't understand what's happening.

Here's what's happening.

I try to unserialize a $UserAccount session object (of class type wsAccount). If there is no object, I create a new instance of wsAccount, and store it in the session.

The first time I visit the page, the object doesn't exist in the session, so a new instance is created and serialized into $_SESSION.

The next time I visit the page the object does exist as expected.

However, if I visit the page again, the object no longer exists, and it is re-created.... and so on.

Here's the code:


<?php

include_once("system/kernel/wsKernel.php");

global $kernel;

$kernel->LoadLibrary("wsAccount");
$kernel->LoadLibrary("wsSession");
$kernel->LoadLibrary("wsTheme");

wsSession::Start();

$theme=new wsTheme();

$UserAccount = unserialize($_SESSION["UserAccount"]);
if($UserAccount != FALSE)
{
echo("Retrieved user account from session: " . $UserAccount->ToString() . "<br>");
}
else
{
echo("Could not retrieve user account<br>");
$UserAccount = wsAccount::FindByEmail("fred@fred.com");
if ($UserAccount != NULL)
{
_echo ("Creating account: " . $UserAccount->ToString() . "<br>");
_$_SESSION["UserAccount"] = serialize($UserAccount);
}
else
{
_echo ("No account found for fred@fred.com");
}
}

?>


I checked my default session path and it is /tmp and the session files are being created. I even checked its contents and it looks ok. Also session variables are maintained between page visits, but not objects. Any ideas?

Thanks for your help.

stodge
05-30-2003, 11:27 AM
If I do this:


echo ("Object $name = ");
print_r($_SESSION[$name]);
echo ("<br>");


I either get:


Object UserAccount = O:9:"wsaccount":1:{s:9:"FirstName";s:4:"Mike";}


or


Object UserAccount = wsaccount Object ( [FirstName] => Mike )


So it seems that it alternates between unserializing the object and not unserializing it. Weirder and weirder.....

Cheers