Re: HTTP_Session - session replication

From: Date: Fri, 15 Sep 2006 17:22:05 +0000
Subject: Re: HTTP_Session - session replication
References: 1 2  Groups: php.pear.dev php.pear.general 
Request: Send a blank email to pear-dev+get-43948@lists.php.net to get a copy of this message
> I'm extending this discussion to per-dev. > > I crafted an extentsion and would appreciate feedback. I have had to > modify > > the HTTP_Session package, as I could not find a better way to extend it > with > > a replicate() behaviour. > > This extension allows the session record to be replicated in another > datatabase table. I'm using MDB2, so I've only extended that module. > Hopefully this replicate() behaviour would be generally useful. > > Perhaps there are better ways to do this (without modifying the PEAR > HTTP_Session package). If not, then perhaps this type of functionality > could be incorporated into the HTTP_Session package, so then I won't need > to > maintain my own copy of it. > > New file: HTTP/Session/Container/MDB2Enhanced.php > Modified file: HTTP/Session.php > > New file: HTTP/Session/Container/MDB2Enhanced.php > --------------------------------------------------------- > <?php > require_once 'HTTP/Session/Container/MDB2.php'; > > class HTTP_Session_Container_MDB2Enhanced extends > HTTP_Session_Container_MDB2 { > function replicate($id, $dest_table) > { > // Check if table row already exists > $query = sprintf("SELECT COUNT(id) FROM %s WHERE id = %s", > $dest_table, > $this->db->quote(md5($id), 'text') > ); > $result = $this->db->queryOne($query); > if (MDB2::isError($result)) { > trigger_error(print_r($result,1)); > $this->db->raiseError($result->code, PEAR_ERROR_DIE); > return false; > } > if (0 == intval($result)) { > // Insert new row into dest table > $query = sprintf("INSERT INTO %s SELECT * FROM %s WHERE id = %s", > $dest_table, > $this->options['table'], > $this->db->quote(md5($id), 'text') > ); > } else { > // Update existing row > $query = sprintf("UPDATE %s dst, %s src SET dst.expiry = > src.expiry, dst.data = src.data WHERE dst.id = src.id AND src.id = %s", > $dest_table, $this->options['table'], > $this->db->quote(md5($id), 'text') > ); > } > $result = $this->db->query($query); > if (MDB2::isError($result)) { > echo $query; > trigger_error('<pre>'.print_r($result,1).'</pre>'); > $this->db->raiseError($result->code, PEAR_ERROR_DIE); > return false; > } > > return true; > } > } > > ?> > > I added the following function to HTTP/Session.php > ----------------------------------------------------- > function replicate($id, $dest_table) { > return $GLOBALS['HTTP_Session_Container']->replicate($id, > $dest_table); > } > > Example Usage > --------------- > ... Take a look at the example code from the HTTP_Session package. Modify > Container_Example.php with the following changes. > > // Use MDB2Enhanced container to get the replicate() behaviour > HTTP_Session::setContainer('MDB2Enhanced', array('dsn' => > 'mysql://root:password@localhost/test', > 'table' => 'sessiondata')); > > // When its time to destroy the session, replicate it first > if (HTTP_Session::isExpired()) { > HTTP_Session::replicate(HTTP_Session::id(), 'cp_sessiondata'); > HTTP_Session::destroy(); > } > > > --- end --- > > LFaR. Good idea, might be useful for others as well. This could be implemented by setting a config option so that all containers would support this natively. Please create a feature request on pear.php.net with links to your modified files and I'll see what I can do and when ;) Thanks and best regards, Torsten

« previous php.pear.dev (#43948) next »