Page 1 of 1

Object in the $_SESSION superglobal

Posted: Mon Aug 09, 2004 10:26 am
by evilmonkey
Hello. I have a class called User, with a constructor that loads all the variables in that class. On login, I assign a new instance of User to $_SESSION['user']:

Code: Select all

<?php
include_once("user_class.php");
//login and all that
session_start();
$_SESSION['user'] = new User;
?>
My problem start when I want to call on a function or a variable in the User class from another page. I do something along these lines:

Code: Select all

<?php
include_once("user_class.php");
session_start();
echo "Printing Started";
echo $_SESSION['user']->username; //supposed to output the username
echo "Printing Finished";
?>
Unfortunatly, the above code produces "Pring StartedPrinting Finished" with nothing in the middle. It does work however if in that same code I put in $_SESSION['user'] = new User (so the object is fine), but I don't want to create a new object on each page, hence the $_SESSION superglobal. Little help?

Thanks!

Posted: Mon Aug 09, 2004 11:04 am
by feyd
hmm.. maybe the object isn't getting all the way across.. try prin_r/var_dump/var_export ing the $_SESSION array on the second page..

to test further, you could create a simple session handler to make sure the data is getting read and written correctly..

Posted: Mon Aug 09, 2004 12:56 pm
by evilmonkey
The $_SESSION has an elemnt called 'id' which is just an integer, that gets read without a problem, that's the wierd part...

Posted: Mon Aug 09, 2004 1:23 pm
by evilmonkey
Okay, here is the output of print_r($_SESSION):

Code: Select all

Array ( &#1111;id] => 1 &#1111;user] => __PHP_Incomplete_Class Object ( &#1111;__PHP_Incomplete_Class_Name] => user &#1111;userid] => &#1111;username] => Vitali &#1111;email] => vitali@vagonweb.com  etc...
Can someone please tell me a _PHP_INCOMPLETE_CLASS Object means?

Thanks!

Posted: Mon Aug 09, 2004 8:30 pm
by evilmonkey
Does anyone know what an incomplete class object is?

Thanks!

Posted: Mon Aug 09, 2004 8:49 pm
by andre_c
I've had that problem before,
What i ended up doing is serializing the object before i saved it to the session, and unserializing it before i used it.
I googled a lot trying to find out about incomplete class object but couldn't find much help on it at that time.

Posted: Mon Aug 09, 2004 8:50 pm
by evilmonkey
What does serializing do?

Posted: Mon Aug 09, 2004 8:51 pm
by andre_c
It turns the whole object ( or array ) into a string that can later be unserialized.

http://us4.php.net/manual/en/function.serialize.php

Posted: Mon Aug 09, 2004 9:59 pm
by evilmonkey
Okay, serializing and unserializing worked. The PHP group really should document this...

Thanks Andre!