Storing Objects in a Session

I am having trouble understanding session variables using OOP PHP.

session auto_start is enabled on the server I am working with and the admin will not change it.

I would like to store several collections of objects in the session. Such as
links object
 - linka object
 - linkb object
 - linkc object

And work it on another page or using xajax. How can I both save and properly test to make sure my objects are being saved and retrieved correctly?

Thanks you advance!

LVL 1
bergstrom_davinAsked:
Who is Participating?
 
blue_hunterTechnical ConsultantCommented:
another alternative way to disable the session.autostart is to disable it file by file, using ini_set() approach
example:

ini_set("session.auto_start", 0);

* at the most top of the page.
0
 
mankowitzCommented:
According to the php docs(http://www.php.net/session)

If you do turn on session.auto_start then you cannot put objects into your sessions since the class definition has to be loaded before starting the session in order to recreate the objects in your session.

However, you may find a way to serialize your objects into a string and store that string and then load your object definition and then deserialize.

0
 
jazahnCommented:
Often when I'm testing to make sure the data went through okay I just give a var_dump($object_var);  That will print the object (to standard out) in a somewhat readable format.  Another way would be to use json_encode($object_var) -- you'll have to use something like that that anyway if you plan on sending the object to javascript.

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
bergstrom_davinAuthor Commented:
I suppose my biggest hurdle is the session will attempt to unserialize on load and create an incomplete object since the definition has yet to be loaded.

Is it possible to disable session autostart on a given page?
0
 
bergstrom_davinAuthor Commented:
function recoverSession() {
      // check to see if session has been started
      if (isset($_SESSION['tmp123']['links'])) {
            $_SESSION['admintools'] = unserialize($_SESSION['tmp123']);
            if (is_object($_SESSION['admintools']['links']) ) return true;
            else return false;
      } else return false;
}

function saveSession() {
      $_SESSION['tmp123'] = serialize($_SESSION['admintools']);
}

This is a function that I have been trying to get working.
0
 
bergstrom_davinAuthor Commented:
Alternatively is there a way to disable auto start for a single folder including subfolders for a windows based server?

I know it can be done on apache servers with a .htaccess folder using php_value session.auto_start 0
0
 
cpulseCommented:
I don't know how you can change the auto_start setting in IIS but your recoverSession() and saveSession() approaches are pretty nice.

You should register them with session_set_save_handler()
Read: http://php.net/session_set_save_handler for full documentation

If I were you I would prefer to unserialize to a global variable though.

function recoverSession() {
      // check to see if session has been started
      if (isset($_SESSION['tmp123']['links'])) {
            $GLOBALS['admintools'] = unserialize($_SESSION['tmp123']);
            if (is_object($GLOBALS['admintools']['links']) ) return true;
            else return false;
      } else return false;
}

function saveSession() {
      $_SESSION['tmp123'] = serialize($GLOBALS['admintools']);
}
0
 
bergstrom_davinAuthor Commented:
will this execute prior to deserializing my objects?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.