Page 1 of 1

Object in session?

Posted: Tue Jan 01, 2008 5:00 am
by Inkyskin
Is it possible to add an object like this in a session?

Code: Select all

$this->imap_connection = imap_open("{".$this->var_server_address."/novalidate-cert/notls}".$this->var_folder, $this->var_username, $this->var_password);
This way I can connect once, and have the connection stay open across many pages untill the session dies. Is that possible, or is it a pipe dream?

Posted: Tue Jan 01, 2008 5:33 am
by devendra-m
yes its possible

Code: Select all

$_SESSION['VAR_NAME']=serialize($objectname);
and

to retrieve

Code: Select all

$objectname=unserialize($_SESSION['VAR_NAME']);
but its going to consume more memory.

Posted: Tue Jan 01, 2008 5:48 am
by Inkyskin
Hi, thanks!

Memory isn't a problem, there will only be me using this control panel.

I have tried what you said, but come up with the error:
Warning: imap_headers(): supplied argument is not a valid imap resource in /home/.smiffy/benshell/linear-perspective.com/flex/includes/php/classes/imap.class.php on line 47
Heres home.php:

Code: Select all

$imap_conn = new bg_imap();
$imap_connect = $imap_conn->connect('208.97.132.128:143','email','password');
$_SESSION['imap_conn'] = serialize($imap_conn);
I want to then still access the connection on the next page, press_emails.php:

Code: Select all

$imap_conn = unserialize($_SESSION['imap_conn']);
$imap_emails = $imap_conn->retrieve_folder();
But that throws the error above... I'm still new to working in an OOP style, so this is all wierd to me, but slowly starting to make sense lol

Posted: Tue Jan 01, 2008 3:52 pm
by Kieran Huggins
While objects can be serialized in a session, "resources" can not.

Also, you can just store them in the session and they'll be automagically saved and restored properly:

Code: Select all

$_SESSION['obj'] = new MyClass;

// page ends here, object saved automagically
//------------------------------------------------------------------
// new request here, object restored automagically

$_SESSION['obj']->runMyMethod();
If you want to store a resource (like an IMAP or DB connection) in an object I'd suggest using a "fetcher" (poor terminology) at the beginning of your methods to guarantee the existence of the connection and return it (untested):

Code: Select all

class MyObject{

   private $imap_connection;
   // ... 

   private function imap_conn(){
      if(!is_resource($this->imap_connection)){
         $this->imap_connection = imap_open(....);
      }
      return $this->imap_connection;
   }

   public function retrieve_folder(){
      $imap = $this->imap_conn();
      // ... do stuff here
   }

}

Posted: Tue Jan 01, 2008 5:09 pm
by Inkyskin
Ah okay thanks,

I was hoping that there was a way to keep the connection open within a session in a sort of persistant state, rather than connecting on every page I need, but I guess I'm out of luck :(

Posted: Tue Jan 01, 2008 6:45 pm
by Kieran Huggins
For databases you can use MySQL_pconnect, but there's no equivalent for generic socket connections like IMAP (as far as I know).