views:

252

answers:

1

I am testing codes from build internet which is a tutorial of OOP. I got a error message:

unserialize() expects parameter 1 to be string, object given in includes/global.inc.php on line 20

Here is the code of serialize():

$_SESSION['user'] = serialize(new User(mysql_fetch_assoc($result)));

And here is the code of unserialize():

$_SESSION['user'] = serialize(new User(mysql_fetch_assoc($result)));

I used Expert Debugger to see what's happening, I found that after user login, the page redirect to index, session variables are still correct, but after the unserialize() in index page was run, all session variables were reset to some numbers, here is the code of unserialize() line:

if(isset($_SESSION["logged_in"])) : $user = unserialize($_SESSION['user']);

I can't figure out what cause this. You can download the whole codes of the program here: http://s3.amazonaws.com/buildinternet/live-tutorials/first-php-app/first-php-application.zip

+3  A: 

I believe that the data might be automatically un-serialized by PHP when you call session_start()

It will automatically be serialized upon the end of execution as well, which means you do not necessarily have to do it yourself.

Chacha102
Correct: `(un)serialize` are not necessary. Assign values or objects directly to keys in `$_SESSION`, and upon session_start(), they'll be there. (With a lot of assumptions involving class definitions being availble, __sleep/wakeup methods).
mrclay
Which means I can remove all (un)serialize functions, right?
Zack