Object in session?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Object in session?

Post 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?
Last edited by Inkyskin on Mon Jan 14, 2008 5:41 am, edited 1 time in total.
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post 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.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post 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
Last edited by Inkyskin on Sun Jan 13, 2008 8:15 am, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
   }

}
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post 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 :(
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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).
Post Reply