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
61 // --- Public properties --- //
65 * Secconds for the session to expire.
69 * @var integer $sessionLife
71 var $sessionLife = AK_SESSION_EXPIRE
;
74 // --- Protected properties --- //
78 * Database instance handler
80 * Stores a reference to an ADODB database instance.
91 * Original session value for avoiding hitting the database in case nothing has changed
96 var $_original_sess_value = '';
101 // ------ CLASS METHODS ------ //
105 // ---- Setters ---- //
108 // {{{ setSessionLife()
111 * $this->sessionLife setter
113 * Use this method to set $this->sessionLife value
116 * @see get$sessionLife
117 * @param integer $sessionLife Secconds for the session to expire.
118 * @return bool Returns true if $this->sessionLife has been set
121 function setSessionLife($sessionLife)
123 $this->sessionLife
= $sessionLife;
130 // ---- Protected methods ---- //
136 * Session open handler
143 $this->_db
=& Ak
::db();
151 * Session close handler
159 * @todo Get from cached vars last time garbage collection was made to avoid hitting db
170 * Session read handler
173 * @param string $id Session Id
178 $query_result = $this->_db
->Execute("SELECT value FROM sessions WHERE id = ".$this->_db
->qstr($id));
179 if(!$query_result && AK_DEBUG
){
180 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
182 $this->_original_sess_value
= (string)$query_result->fields
[0];
183 return $this->_original_sess_value
;
192 * Session write handler
196 * @param string $data
199 function _write($id, $data)
201 // We don't want to hit the db if nothing has changed
202 if($this->_original_sess_value
!= $data){
203 $ret = $this->_db
->Replace('sessions', array('id'=>$this->_db
->qstr($id),'expire'=>$this->_db
->DBTimeStamp(time()),'value'=>$this->_db
->qstr($data)), 'id');
218 * Session destroy handler
224 function _destroy($id)
226 if(!$this->_db
->Execute('DELETE FROM sessions WHERE id = '.$this->_db
->qstr($id)) && AK_DEBUG
){
227 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
229 return (bool)@$this->_db
->Affected_Rows();
236 * Session garbage collection handler
243 if(!$this->_db
->Execute('DELETE FROM sessions WHERE expire < '.$this->_db
->DBTimeStamp(time()-$this->sessionLife
)) && AK_DEBUG
){
244 trigger_error($this->_db
->ErrorMsg(), E_USER_NOTICE
);
246 return (bool)$this->_db
->Affected_Rows();
254 }// End of if(!defined('AK_DBSESSION_CLASS_INCLUDED')){