Zack
Zack

Reputation: 397

PHP SESSION: SESSION Variables Automatically Reset after Unserialize()

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

Upvotes: 1

Views: 2759

Answers (2)

Italo Souza
Italo Souza

Reputation: 1

Old post, but if you still looking for it, try:

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

Upvotes: 0

Tyler Carter
Tyler Carter

Reputation: 61577

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.

Upvotes: 4

Related Questions