CodeIgniter Forums
Storing object in a session - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Storing object in a session (/showthread.php?tid=8980)



Storing object in a session - El Forum - 06-07-2008

[eluser]Keago[/eluser]
Hi all,

I'm using the native session library (autoloaded) supplied in the codeigniter package. I stumbled upon a problem when storing objects in the session. Because the session is started before the php file containing the object is included, the object can't be unserialized resulting in an error.

I solved it by hooking into the pre_system hook. But I prefer to hook in exactly before the session is started. So far I have not seen any possible way to do this without modifying the Session library code. Am I overlooking something or is it just not possible? There must be a nicer way to do this, I guess...

It would be nice if the session class provided a setting for unserializing objects on the fly, instead of immediately at instantiation time.

Thnx.


Storing object in a session - El Forum - 06-08-2008

[eluser]gtech[/eluser]
out of curiosity may I ask why you want to store an object in the session? I think you are restricted to 4kb.


Storing object in a session - El Forum - 06-09-2008

[eluser]Keago[/eluser]
Of course. The object I store is just a plain data object without any methods, their isn't any particular reason except that I find it a convenient way to restore the data directly into an object.


Storing object in a session - El Forum - 06-09-2008

[eluser]gtech[/eluser]
Code:
<?php
class foo
{
  function foo(){
    $this->vartest = "hi";
  }
}

class Temp extends Controller {

  function Temp()
  {
    parent::Controller();
  }
  function index()
  {
      $resobj = new foo;
      $newdata = array('resobj' => $resobj);
      $this->session->set_userdata($newdata);
  }
  function page2()
  {
      $retobj = $this->session->userdata('resobj');
      print_r($retobj);
  }
}
?>

the above works for me (I also autoload session) I don't know if the code helps you.