| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 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 // +----------------------------------------------------------------------+ 10 11 /** 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> 17 */ 18 19 if(!defined('AK_DBSESSION_CLASS_INCLUDED')){ define('AK_DBSESSION_CLASS_INCLUDED',true); // Class overriding trick 20 21 22 require_once (AK_LIB_DIR.DS.'Ak.php'); 23 require_once (AK_LIB_DIR.DS.'AkObject.php'); 24 25 26 /** 27 * Database based session. 28 * 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. 32 * 33 * <code> 34 * 35 * require_once(AK_LIB_DIR.'/AkDbSession.php'); 36 * 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') 46 * ); 47 * 48 * </code> 49 * 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> 53 * @since 0.1 54 * @deprecated use AkSession instead 55 * @version $Revision 0.1 $ 56 */ 57 class AkDbSession extends AkObject 58 { 59 /** 60 * Secconds for the session to expire. 61 * 62 * @see setSessionLife 63 * @access public 64 * @var integer $sessionLife 65 */ 66 var $sessionLife = AK_SESSION_EXPIRE; 67 68 /** 69 * Database instance handler 70 * 71 * Stores a reference to an ADODB database instance. 72 * 73 * @access protected 74 * @var object $_db 75 */ 76 var $_db; 77 78 /** 79 * Original session value for avoiding hitting the database in case nothing has changed 80 * 81 * @access private 82 * @var string $_db 83 */ 84 var $_original_sess_value = ''; 85 86 /** 87 * $this->sessionLife setter 88 * 89 * Use this method to set $this->sessionLife value 90 * 91 * @access public 92 * @see get$sessionLife 93 * @param integer $sessionLife Secconds for the session to expire. 94 * @return bool Returns true if $this->sessionLife has been set 95 * correctly. 96 */ 97 function setSessionLife($sessionLife) 98 { 99 $this->sessionLife = $sessionLife; 100 101 } 102 103 // ---- Protected methods ---- // 104 105 /** 106 * Session open handler 107 * 108 * @access protected 109 * @return boolean 110 */ 111 function _open() 112 { 113 $this->_db =& Ak::db(); 114 return true; 115 } 116 117 /** 118 * Session close handler 119 * 120 * @access protected 121 * @return boolean 122 */ 123 function _close() 124 { 125 /** 126 * @todo Get from cached vars last time garbage collection was made to avoid hitting db 127 * on every request 128 */ 129 $this->_gc(); 130 return true; 131 } 132 133 /** 134 * Session read handler 135 * 136 * @access protected 137 * @param string $id Session Id 138 * @return string 139 */ 140 function _read($id) 141 { 142 $result = @$this->_db->selectValue("SELECT value FROM sessions WHERE id = ".$this->_db->quote_string($id)); 143 return is_null($result) ? '' : (string)$result; 144 } 145 146 /** 147 * Session write handler 148 * 149 * @access protected 150 * @param string $id 151 * @param string $data 152 * @return boolean 153 */ 154 function _write($id, $data) 155 { 156 // We don't want to hit the db if nothing has changed 157 if($this->_original_sess_value != $data){ 158 /** 159 * @todo replace with dbAdapter-method 160 */ 161 $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'); 162 if($ret == 0){ 163 return false; 164 }else{ 165 return true; 166 } 167 }else { 168 return true; 169 } 170 } 171 172 /** 173 * Session destroy handler 174 * 175 * @access protected 176 * @param string $id 177 * @return boolean 178 */ 179 function _destroy($id) 180 { 181 return (bool)@$this->_db->delete('DELETE FROM sessions WHERE id = '.$this->_db->quote_string($id)); 182 } 183 184 /** 185 * Session garbage collection handler 186 * 187 * @access protected 188 * @return boolean 189 */ 190 function _gc() 191 { 192 return (bool)@$this->_db->delete('DELETE FROM sessions WHERE expire < '.$this->_db->quote_datetime(time()-$this->sessionLife)); 193 194 } 195 196 197 } 198 199 }// End of if(!defined('AK_DBSESSION_CLASS_INCLUDED')){ 200 201 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |