simplyi Posted March 26, 2010 Share Posted March 26, 2010 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! Quote Link to comment Share on other sites More sharing options...
oni-kun Posted March 26, 2010 Share Posted March 26, 2010 If the object is more a 'resource', which you cannot store in a session. A solution is to copy all relevant data to a 'normal' array or object, or if you're able to, you can also serialize it. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 26, 2010 Share Posted March 26, 2010 If the class definition exists before the session_start() statement, what you are doing should work. What is your code? Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 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! Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 //using typecasting as a shortcut around that error $_SESSION['bookOffers'] = (string)$bookOffers; Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 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? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 what does it output like array=>() ??? if so you could do print_r() to output the session variables Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 $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; } Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 you have it backwards put the session as the bookoffer not the other way $_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; } Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 :) 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? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 // 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; } Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 $_SESSION['bookOffers'] = (object)$bookOffers; Generates Warning message ;( ( ! ) Warning: session_start() [function.session-start]: Node no longer exists Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 when you put (string) does it work Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 yes, when I cast it to String it gets stored in $_SESSION and no Warning message... But then I cannot cast it back from String to Array. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 if it is array why not use $bookOffers = (array)$_SESSION['bookOffers']; Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 26, 2010 Share Posted March 26, 2010 ALso this might help on how to use caste types http://www.phpro.org/tutorials/PHP-Type-Casting.html#4.3 Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 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; Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 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... Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 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 Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 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. Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 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; } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 26, 2010 Share Posted March 26, 2010 $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). Quote Link to comment Share on other sites More sharing options...
simplyi Posted March 26, 2010 Author Share Posted March 26, 2010 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. ... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.