villeit Posted October 18, 2009 Share Posted October 18, 2009 Hi all, I am getting really frustrated with storing and retreiving objects from a session. But it may be because of my lack of knowledge in sessions First some system specs: OS - Vista Home 32bit Webserver - IIS 7 PHP - PHP 5.2.11 php.ini session.auto_start = 1 So, when I decide to store an object array in the session, it works just fine. I can also retrieve it from the session but with an extra attribute mentioning: "__PHP_Incomplete_Class_Name" with the value of the Object Name. Code when I try to retrieve object from session: $hits = $_SESSION['hitsArray']; // Array of MyObject AMethodThatDoesSomething($hits[0]); // Requires MyObject Type And this is where it fails: I get following information in the debugger: __PHP_Incomplete_Class_Name | MyObject Attribute 1 | A value Attribute 2 | Another Value ....And so on.... Ok, so I did some research and found out objects do not work if you have "session.auto_start = 1" in php.ini AND that you need to add following line above your session_start(): require_once("MyObject.php"); session_start(); header("Cache-control: private"); session_register('hitsArray'); // Array of MyObject $_SESSION['hitsArray'] = $hits; // Store into session So, I changed "session.auto_start = 0" in php.ini and added the required line above. Well then it didn't even store my object array in the session in the first place! Now I feel that I am stumbling in the dark and hope for someone to be able to help me in my ordeal! Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/ Share on other sites More sharing options...
MadTechie Posted October 18, 2009 Share Posted October 18, 2009 How are you setting the object to the session ? ie session_start(); header("Cache-control: private"); require_once("MyObject.php"); $obj = new myObject(); $_SESSION['hitsArray'] = $obj->hits; // Store into session echo $_SESSION['hitsArray'][0]; Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-939134 Share on other sites More sharing options...
villeit Posted October 18, 2009 Author Share Posted October 18, 2009 The "$hits" variable is an array of MyObject. So I am essentially inserting the whole array of objects into the Session variable.. . function SaveToSession($hits){ session_start(); header("Cache-control: private"); //IE 6 Fix require_once("tiers/Person.php"); session_register('hitsArray'); $_SESSION['hitsArray'] = $hits; // Save hits into session } Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-939316 Share on other sites More sharing options...
MadTechie Posted October 18, 2009 Share Posted October 18, 2009 That doesn't help much as your passing $hits to a function that sets it to a session.. which is as useful as saying theirs a problem with this line $_SESSION['hitsArray'] = $hits; I would really need more info or an example. also remove session_register session_register This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-939334 Share on other sites More sharing options...
villeit Posted October 18, 2009 Author Share Posted October 18, 2009 Ok, the reason I am putting an array of hits into a session is that I can easily delete after I have displayed a hitlist to the user. From that hitlist user is able to select a row for deletion. When the page is posted for deletion, I will retrieve the array from the Session and delete the expected index. // Check that the search form has been submitted if (isset($_POST['search'])) { // After user has clicked submit $firstName = $_POST['fnamn']; $lastName = $_POST['enamn']; // Create a query object $query = new Query($firstName, $lastName); $bl = new BL(); // Returns an array of person $hits = $bl->FindPerson($query); if(count($hits) < 1){ echo "<p>Tyvärr, inga träffar på din sökning.</p><br>"; }else{ SaveToSession($hits); DisplayResults($hits); } } // Check that the erase form has been submitted if (isset($_POST['erase'])) { // Get index to delete $index = $_POST['radiobutton']; // retrieve persons from session $hits = $_SESSION['hitsArray']; $bl = new BL(); // Pass person for deletion $result = $bl->ErasePerson($hits[$index]); // Erase the selected person echo "Deletion happened: " . $result; } Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-939357 Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2009 Share Posted October 18, 2009 Have you even determined that sessions are working for any $_SESSION variable? Then go onto the next step of storing an object in one. Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-939360 Share on other sites More sharing options...
villeit Posted October 19, 2009 Author Share Posted October 19, 2009 ok I solved it now with serialization. Its not ideal, but at least it works! // Serialize $serializedhits = serialize($hits); session_start(); header("Cache-control: private"); //IE 6 Fix $_SESSION['hitsarray'] = $serializedhits; // Save hits into session // Unserialize $hits = $_SESSION['hitsarray']; // Person array from Session $hitsFromSess = unserialize($hits); unset($_SESSION['hitsarray']); // Delete session variable Its a bit of shame that you can´t store objects straight into a session even if you have the session.auto_start = 1 in php.ini Or maybe its just me having this problem Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-939546 Share on other sites More sharing options...
MadTechie Posted October 19, 2009 Share Posted October 19, 2009 Technically any data in a session is serialized Link to comment https://forums.phpfreaks.com/topic/178111-solved-retreiving-objects-from-sessions/#findComment-940052 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.