$_SESSION[Object] is ok?

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
dwnthk
Forum Newbie
Posts: 13
Joined: Mon Oct 08, 2007 5:55 am

$_SESSION[Object] is ok?

Post by dwnthk »

Hi there,

I have a class called PageImp.

In the first page, I have a variable called $sopage which got the type of PageImp class.

***** PLEASE USE TAGS WHEN POSTING CODE *****

Code: Select all

$sopage = new PageImp();
$_SESSION['sopage'] = $sopage;
......

In the other page, I try to retrieve the object like:

Code: Select all

$so2page = new PageImp();
$so2page = $_SESSION['sopage'];
.....

I got $so2page empty.

Can I pass the object in this way? If I can't, how can I pass the object between pages?

Thanks.

Dave
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

if(!isset($_SESSION['sopage'])) $_SESSION['sopage'] = new PageImp();
should work both places.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $_SESSION[Object] is ok?

Post by Christopher »

Remember, both sessions and objects need something before they can be used. And if you store object in the session then the class needs to exist before the session is started.

Code: Select all

include '/path/to/PageImp.php';
session_start();

$sopage = new PageImp();
$_SESSION['sopage'] = $sopage;
and:

Code: Select all

include '/path/to/PageImp.php';
session_start();

$so2page = $_SESSION['sopage'];
(#10850)
dwnthk
Forum Newbie
Posts: 13
Joined: Mon Oct 08, 2007 5:55 am

Re: $_SESSION[Object] is ok?

Post by dwnthk »

arborint wrote:Remember, both sessions and objects need something before they can be used. And if you store object in the session then the class needs to exist before the session is started.

Code: Select all

include '/path/to/PageImp.php';
session_start();

$sopage = new PageImp();
$_SESSION['sopage'] = $sopage;
and:

Code: Select all

include '/path/to/PageImp.php';
session_start();

$so2page = $_SESSION['sopage'];
Thanks. It works. :D
I am not sure why I don't need to create the $so2page before I can assign the object to it. It is the way of PHP does maybe?!



Dave
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $_SESSION[Object] is ok?

Post by Christopher »

dwnthk wrote:I am not sure why I don't need to create the $so2page before I can assign the object to it. It is the way of PHP does maybe?!
You create the object in the first script. You assign it into the $_SESSION superglobal array. So when the first script ends, PHP serialize()'s all the data in $_SESSION and writes the data to whatever datastore your session is using (to disk by default). For objects is saves the class name.

Then in the second script when you start the session it reads the data from the datastore and unserialize()'s it. If there is an object in the data it checks to see if the class is loaded and if it is, it fixes up the object just like new. If the class is not loaded it marks the object class as unknown.
(#10850)
dwnthk
Forum Newbie
Posts: 13
Joined: Mon Oct 08, 2007 5:55 am

Post by dwnthk »

I got this error message when I tried to pass the object to the other script:

Code: Select all

Fatal error: main() [<a href='function.main'>function.main</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "PageImp" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\Program Files\Apache Group\Apache2\htdocs\test\pmc_test1.php on line 30
I did not do any serialize() function in my php. Is that because the php will do serialize() by itself?

After that, then I tried to add serialize() and unserialize() to my scripts, but it seems like serialize()/unserialize() only take string as the parameter. Is it correct?

Thanks.
dwnthk
Forum Newbie
Posts: 13
Joined: Mon Oct 08, 2007 5:55 am

Post by dwnthk »

Gezze... It seems working now. What I did is to swap the following lines:

Code: Select all

session_start();
include ('PageImp.php');

:lol: :lol: :lol:
Post Reply