Welcome Guest, Not a member yet? Register   Sign In
Storing object in a session
#1

[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.
#2

[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.
#3

[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.
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB