Object in the $_SESSION superglobal

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Object in the $_SESSION superglobal

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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...
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Does anyone know what an incomplete class object is?

Thanks!
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

What does serializing do?
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Okay, serializing and unserializing worked. The PHP group really should document this...

Thanks Andre!
Post Reply