Jump to content

[SOLVED] Retreiving objects from Sessions


villeit

Recommended Posts

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  :P

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! :wtf:

 

Now I feel that I am stumbling in the dark and hope for someone to be able to help me in my ordeal! :D

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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 :P

 

 

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.