?
Solved

Objects stored in PHP session?

Posted on 2003-03-26
17
Medium Priority
?
1,026 Views
Last Modified: 2011-09-20
Hi all,

Is it possible to store a complex object in the session?

class {

var $arra; //10 elements
var
....

}

it doesn't work by me. what could be a workaround?
I can only store simple variables in the session objects, arrays also doesn't work....

Regards,
pret
0
Comment
Question by:pret32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +3
17 Comments
 
LVL 15

Expert Comment

by:VGR
ID: 8211958
yes

and arrays DO WORK

I use them all the time 8-)
0
 
LVL 8

Expert Comment

by:alakriti
ID: 8212065
it does work in fact on most ecommerce applications i have made i use this to store the contents of the shopping cart.

$cart = new Cart();

session_register("cart");

$_SESSION['cart']->AddItem();


etc... arrays work the same way

$array = array();

session_register("array");

$_SESSION['array']['0'];
$_SESSION['array']['1'];

etc etc...

0
 
LVL 11

Expert Comment

by:shmert
ID: 8213219
Storing (serializing) objects in session should work fine, but you need to always include the class definition before trying to do anything with the serialized object.  Only the object variables are saved in the session.

Also, the default behavior for PHP is to save the entire session variable to file after each page hit, usually in the /tmp directory.  So, if you are going to be storing an Object in your session, it's a good idea to try to keep it small.  The best way to do this is just reduce the number of object variables, and don't store huge arrays in your object.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pret32
ID: 8216470
When i stored a class object that contained array and other variables, seem to work (no error messages), but when i said $var=$_SESSION['class object'] and then $var->method() i get the following error message:

Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition cheider of the object you are trying to operate on was loaded _before_ the session was started in bspern.php on line 26


what have i overseen?
0
 

Author Comment

by:pret32
ID: 8216483
you mean that when i am only doing $var=$_SESSION['class object'] and then $var->method() in script i should make include('classdefinition.php'); before the line of $var...?
0
 
LVL 15

Expert Comment

by:VGR
ID: 8216628
the object should be completely 'filled in" before being registered via $_SESSION['name']=$refObject;
0
 

Author Comment

by:pret32
ID: 8216770
it is completely filled before $_SESSION['var']=$var;

but what about this error message?
0
 
LVL 9

Expert Comment

by:waygood
ID: 8217926
Have you included the class BEFORE you start the session on the new page?

You have to tell php what it is before you reload it into memory, otherwise it doesn't know how to handle it.
0
 
LVL 7

Expert Comment

by:a1programmer
ID: 8218562
Yes.

Do this...

when your done using it/filling it on a page...
// save code
$_SESSION["var"]  = serialize($var);


When you want to retrieve it for use on another page.
// retrieve code
$temp = $_SESSION["var"];
$var = unserialize($temp);



This breaks down complex structured data types into a basic linear/contiguous data type.

You can also do this to store object data in session variables...


Hope this helps...
0
 

Author Comment

by:pret32
ID: 8223494
I added with require function the class definition before i read the class object from the session and he says

Fatal error: Call to undefined function: getname()

what's wrong?
0
 
LVL 7

Expert Comment

by:a1programmer
ID: 8224867
let us see your code...
0
 
LVL 7

Expert Comment

by:a1programmer
ID: 8225723
Your not going to have a valid object unless your serializing and unserializing it/ while storing it as a session...  That would give you that error...

Check out this article...

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=809&lngWId=8
0
 

Author Comment

by:pret32
ID: 8261650
Before session_start i include the class definitions and when i read a complex object from the session and then access the method of an object that is stored in the complex object and i get the error message that i am calling the undefined function. What's wrong?
I cannot solve my problem....
0
 
LVL 7

Expert Comment

by:a1programmer
ID: 8261738
Are my comments not showing up?  
0
 

Author Comment

by:pret32
ID: 8261767
yeah, but when i simply serialize and unserialize it it doesn't make any difference!
0
 

Author Comment

by:pret32
ID: 8261827
if the complex object ist read and then by access method to delegated object comes the problem and class definitions are added before session_start what else could be wrong.....
0
 
LVL 11

Accepted Solution

by:
shmert earned 90 total points
ID: 8263367
To debug your problem, try this:
assuming you've registered an object in the session on some previous page using the line: $_SESSION['foo'] = new MyCoolClass();

print_r($_SESSION);

Then view the source to see all your session variables.  If the session says something like

[foo] => __PHP_Incomplete_Class Object

then this is because you have not included the source file that defines the object stored in the session at $_SESSION['foo'].  You need to do this include() before calling session_start().

if it says

[foo] => MyCoolClass Object

then the session variable contains the valid class, which you can call methods on.  No serialization is required!

If MyCoolClass has a method called "setName()", you can use the following code:

$obj = &$_SESSION['foo']; // get a reference for the object
$obj->setName("bobo");

and this change will be saved in the session.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question