pass Object through sessions PHP 5

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
ehsun7b
Forum Newbie
Posts: 8
Joined: Tue Nov 22, 2005 12:33 am
Contact:

pass Object through sessions PHP 5

Post by ehsun7b »

Is it possible to put Objects in the $_SESSION, $_POST or $_GET in PHP 5? If so, how should I recover them after putting???

Please explain it
:?:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are we to assume that you've tried it and failed?
User avatar
ehsun7b
Forum Newbie
Posts: 8
Joined: Tue Nov 22, 2005 12:33 am
Contact:

Post by ehsun7b »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Yeah
I put an object in the $_SESSION and when I was trying to recover it in the next page I faced this error:

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 "Role" 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\epeyk\class\_test2.php on line 10
What should I do? I print_r the $_SESSION in the target page and it contains my object in a strange stucture:

Code: Select all

Array
(
    [role] => __PHP_Incomplete_Class Object
        (
            [__PHP_Incomplete_Class_Name] => Role
            [id:private] => 1
            [title:private] => Administrator
            [description:private] => This role has all the permissions
        )

)

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

you need to include the class definition for both pages that will use the object.

page1:

Code: Select all

<?php
session_start();

include 'MyClass.php';

$_SESSION['object'] = new MyClass();

?>
page 2:

Code: Select all

<?php
session_start();

include 'MyClass.php';

$_SESSION['object']->myMethod();

?>
User avatar
ehsun7b
Forum Newbie
Posts: 8
Joined: Tue Nov 22, 2005 12:33 am
Contact:

Post by ehsun7b »

I've done this before. :(

Is there any other specification!!!

What about $_POST and $_GET??? :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The class definition must be present before the object is rebuilt from the session data.

Code: Select all

include 'MyClass.php';
session_start();
$_SESSION['object']->myMethod();
Post Reply