2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
12 * @package ActionController
13 * @subpackage Sessions
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
19 if(!defined('AK_DBSESSION_CLASS_INCLUDED')){ define('AK_DBSESSION_CLASS_INCLUDED',true); // Class overriding trick
22 require_once(AK_LIB_DIR
.DS
.'Ak.php');
23 require_once(AK_LIB_DIR
.DS
.'AkObject.php');
27 * Database based session.
29 * This class enables saving sessions into a database. This can
30 * be usefull for multiple server sites, and to have more
31 * control over sessions.
35 * require_once(AK_LIB_DIR.'/AkDbSession.php');
37 * $AkDbSession = new AkDbSession();
38 * $AkDbSession->sessionLife = AK_SESSION_EXPIRE;
39 * session_set_save_handler (
40 * array(&$AkDbSession, '_open'),
41 * array(&$AkDbSession, '_close'),
42 * array(&$AkDbSession, '_read'),
43 * array(&$AkDbSession, '_write'),
44 * array(&$AkDbSession, '_destroy'),
45 * array(&$AkDbSession, '_gc')
50 * @author Bermi Ferrer <bermi at akelos com>
51 * @copyright Copyright (c) 2002-2005, Akelos Media, S.L. http://www.akelos.org
52 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
54 * @version $Revision 0.1 $
56 class AkDbSession
extends AkObject
59 * Secconds for the session to expire.
63 * @var integer $sessionLife
65 var $sessionLife = AK_SESSION_EXPIRE
;
68 * Database instance handler
70 * Stores a reference to an ADODB database instance.
78 * Original session value for avoiding hitting the database in case nothing has changed
83 var $_original_sess_value = '';
86 * $this->sessionLife setter
88 * Use this method to set $this->sessionLife value
91 * @see get$sessionLife
92 * @param integer $sessionLife Secconds for the session to expire.
93 * @return bool Returns true if $this->sessionLife has been set
96 function setSessionLife($sessionLife)
98 $this->sessionLife
= $sessionLife;
102 // ---- Protected methods ---- //
105 * Session open handler
112 $this->_db
=& Ak
::db();
117 * Session close handler
125 * @todo Get from cached vars last time garbage collection was made to avoid hitting db
133 * Session read handler
136 * @param string $id Session Id
141 $result = $this->_db
->selectValue("SELECT value FROM sessions WHERE id = ".$this->_db
->quote_string($id));
142 return is_null($result) ?
'' : (string)$result;
146 * Session write handler
150 * @param string $data
153 function _write($id, $data)
155 // We don't want to hit the db if nothing has changed
156 if($this->_original_sess_value
!= $data){
158 * @todo replace with dbAdapter-method
160 $ret = $this->_db
->connection
->Replace('sessions', array('id'=>$this->_db
->quote_string($id),'expire'=>$this->_db
->quote_datetime(time()),'value'=>$this->_db
->quote_string($data)), 'id');
172 * Session destroy handler
178 function _destroy($id)
180 return (bool)$this->_db
->delete('DELETE FROM sessions WHERE id = '.$this->_db
->quote_string($id));
184 * Session garbage collection handler
191 return (bool)$this->_db
->delete('DELETE FROM sessions WHERE expire < '.$this->_db
->quote_datetime(time()-$this->sessionLife
));
197 }// End of if(!defined('AK_DBSESSION_CLASS_INCLUDED')){