Jump to content

cannot store an array of objects in session?


simplyi

Recommended Posts

Hello.

 

I have an array of 60+ objects which I try to put into user session to have it available while user is browsing web site. Each object in an array contains some data. Can it be that the array of objects I am trying to keep in session is too large? because I am getting this working message as soon as I put my array into session:

 

Warning: session_start() [function.session-start]: Node no longer exists

 

I put other data like Strings into $_SESSION and it works well.

 

Is it possible to solve the problem with an array of objects and Warning message? What other ways are there in PHP to persist large objects? I do not want to store the data I keep in array into a database temp table...

 

 

Thank you!

Link to comment
Share on other sites

Thank you very much for your replies!

The only class in my Array is BookVO.php and I tried declaring it before session_start(). Does not help.

 

The code is large but here its short structure.

 

require_once `BookVO.php`;

session_start();

 

// function findBook returns an Array of two Maps.

//$searchResults[0]  -- Holds a Map of:  RecordId(as key) => BookValueObject(as Value)

//$searchResults[1]  -- Holds a Map of:  BookPrice(as key) =>  BookValueObject(Value)

 

$bookOffers = $storesDAO->findBookOffers($isbn);

 

// if I add $bookOffers into $_SESSION I will get WARNING message mentioned earlier

$_SESSION['bookOffers'] = $bookOffers;

 

Thank you!

Link to comment
Share on other sites

Thank you!

 

This solves the problem with WARNING message:

 

$_SESSION['bookOffers'] = (string)$bookOffers;

 

but now I cannot get my Array back to normal.

 

$bookOffers = $_SESSION['bookOffers']

 

or

 

$bookOffers = (Array)$_SESSION['bookOffers']

 

Does not help ;(

 

How to cast back?

Link to comment
Share on other sites

$bookOffers = $_SESSION['bookOffers']

 

// print_r returns a string:  Array

print_r($bookOffers);

 

// returns:  1

echo count($bookOffers);

 

// Generated Warning message: Warning: Invalid argument supplied for foreach()

foreach($bookOffers as $key=>$value)

{

    echo "<br/>" . $key . "=" . $value;

}

 

 

Link to comment
Share on other sites

you have it backwards put the session as the bookoffer not the other way 8)

 

 

 $_SESSION['bookOffers'] = $bookOffers;

// print_r returns a string:  Array
print_r($_SESSION['bookOffers']);

// returns:  1
echo count($_SESSION['bookOffers']);

// Generated Warning message: Warning: Invalid argument supplied for foreach()
foreach($_SESSION['bookOffers'] as $key=>$value)
{
     echo "<br/>" . $key . "=" . $value;
}

 

 

Link to comment
Share on other sites

:) :) I am very sorry for this confusion but of course I do it the other way around :). Here is my example:

 

// First I do The Cast and store in $_SESSION

$_SESSION['bookOffers'] = (String)$bookOffers;

 

// Second I read from $_SESSION

$bookOffers = $_SESSION['bookOffers'];

 

// Then I try to debug

// print_r returns a string:  Array

print_r($bookOffers);

 

// returns:  1

echo count($bookOffers);

 

// Generated Warning message: Warning: Invalid argument supplied for foreach()

foreach($bookOffers as $key=>$value)

{

    echo "<br/>" . $key . "=" . $value;

}

 

 

:( does not help... What is the way around?

Link to comment
Share on other sites

// First I do The Cast and store in $_SESSION
$_SESSION['bookOffers'] = (object)$bookOffers;



// Then I try to debug
// print_r returns a string:  Array
print_r( $_SESSION['bookOffers']);

// returns:  1
echo count( $_SESSION['bookOffers']);

// Generated Warning message: Warning: Invalid argument supplied for foreach()
foreach( $_SESSION['bookOffers'] as $key=>$value)
{
     echo "<br/>" . $key . "=" . $value;
}

Link to comment
Share on other sites

when an array is cast, or coerced into a string, all the elements are ignored all together, and the string becomes the literal string 'array'. There is no way to convert it back to its original array (even if you try casting it as an array). All casting the literal string 'Array' into an array will do is make a 1 element array with a the string 'Array' as its element. This is why when you do

$array = array(...);//an array with some elements
echo $array;

all that gets echoed is 'Array'. The command echo needs a string, so anything that is to the right of the command is coerced into one. Most types in PHP can do this quite seemlessly, but objects usually have some trouble. This is why print_r and var_dump are useful

 

consider another example

$a = array();
for ($i = 0; $i < 2; $i++){
$a[] = $i;
}
$b = (string)$a;
echo $b;//echos: Array
$c = (array)$b;

print_r($c);//prints: Array ( [0] => Array )

 

one option is to use serialize() and unserialize, which turns an object into a string representation (which you can then unserialize to turn back into its object)

 

However, based on your original problem, you simply want to store an array of objects into the session array yes?

 

as has been said before, as long as you have the class definition (IE include the class file) before session start, you should be able to pass objects in a session from page to page. The fact that they are in an array doesn't matter. Hell, $_SESSION is an array.

 

for example, the following should work

include("foo.php");//assume this has some class foo
session_start();
$foo = new Foo();

$_SESSION['foo'] = $foo;

//this also should work;
$foos = array();
for ($i = 0; $i < 3; $i++){
$foos[] = new Foo();
}

$_SESSION['foos'] = $arr;

 

 

 

 

Link to comment
Share on other sites

Yes, I does make big sense! :) and I did try that :(

 

It generates the Warning message.

 

Warning: session_start() [function.session-start]: Node no longer exists

 

The only way when Warning message is not generated is when I cast to String. But then I cannot get it back to Array.

 

Fooooh...  :confused:

Link to comment
Share on other sites

Yes, when I store an Object of Class Foo in $_SESSION then it works... but not my Array :(

 

What is the correct way to serialize an Array and store it in $_SESSION? The example bellow I have also tried and it also generates  Warning message.

 

$_SESSION['bookOffers'] = serialize($bookOffers);

 

//and then get it back

$bookOffers = unserialize($_SESSION['bookOffers']);

 

 

Warning: unserialize() [function.unserialize]: Node no longer exists

 

 

:confused:

 

Link to comment
Share on other sites

interesting. what type of object are you trying to store into an array?after a quick google search it seems that "resources" CANNOT be stored in the session array while regular objects can, and many people attribute this to why they get your specific error.

Link to comment
Share on other sites

The structure is as mentioned above:

 

require_once `BookVO.php`;

session_start();

 

// function findBook returns an Array of two Maps.

//$searchResults[0]  -- Holds a Map of:  RecordId(as key) => BookValueObject(as Value)

//$searchResults[1]  -- Holds a Map of:  BookPrice(as key) =>  BookValueObject(Value)

 

the BookVO is a Class with setters and getters functions. For example.

 

function setTitle($title)

{

  $this->title = $title

}

 

function getTitle ()

{

return $this->title;

}

Link to comment
Share on other sites

$bookOffers = $storesDAO->findBookOffers($isbn);

 

Based on just the naming being used (since you have not bothered to post the class definition), $storesDAO is likely a dynamically produced result object that references the result resource of a query. As has been stated twice before in this thread, there are some things that cannot be stored in a session -

Warning

Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object).

 

A possible solution would be to use a class destructor to take the data you need (assuming it is not actually a resource) and put it into session variable(s).

Link to comment
Share on other sites

PFMaBiSmAd, thank you very much for your reply.

 

No, an Array of two Maps that I am trying to store in Session does not Contain a resource like ResultSet after running SQL query. The class used to return BookOffers IS called DAO but it does not work with a Database but works as kind of Data Access Object to query different Web Services to get XML responses, parse them, build BookVO objects(get() and set() methods) and return an Array of these BookVOs stored in a Map(Key, Value). Then the two Maps are added to one $returnValue array and this $returnValue array is returned back as a result of function call. Because there is a long business logic behind the scenes and many lines of code I do not post the entire method here but only a sample structure of an Array I am trying to put into $_SESSION.

...

:confused:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.