1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| ---- session.sql ----
CREATE TABLE `sessions` (
`id` varchar(32) collate latin1_general_ci NOT NULL,
`access` int(10) default NULL,
`data` text collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
---- bootstrap (index.php) ----
...
Zend_Loader::loadClass('Zend_Db');
Zend_Loader::loadClass('Zend_Session');
Zend_Loader::loadClass('Concentre_Session_SaveHandler_Db');
$params = array() /* paramètres de connexion a la base de données */
$db = Zend_Db::factory('PDO_MYSQL', $params );
Zend_Session::setSaveHandler(new Concentre_Session_SaveHandler_Db($db));
...
---- library/Concentre/Session/SaveHandler/Db.php ----
<?php
class Concentre_Session_SaveHandler_Db implements Zend_Session_SaveHandler_Interface {
private $_session;
public $maxTime;
private $db;
public function __construct($database) {
$this->db = $database;
$this->maxTime['access'] = time();
$this->maxTime['gc'] = get_cfg_var('session.gc_maxlifetime');
/*
session_set_save_handler(array($this, '_open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destroy'),
array($this, 'gc')
);*/
register_shutdown_function('session_write_close');
}
public function open($save_path, $name)
{
$this->gc( $this->maxTime['gc'] );
return true;
}
public function close()
{
$this->gc($this->maxTime['gc']);
return true;
}
public function read($id)
{
$allData = $this->db->fetchRow("SELECT `data` FROM `sessions` WHERE `id` = :id",array('id' => $id));
$hasData = (bool) $allData != null;
return $hasData ? $allData['data'] : '';
}
public function write($id, $data)
{
$set = array(
'id' => $id,
'access' => $this->maxTime['access'],
'data' => $data
);
$where = $this->db->quoteInto("`id` = ?", $id);
$rows_affected = $this->db->update('sessions', $set, $where);
if ($rows_affected != 1) {
$rows_affected = $this->db->insert('sessions', $set);
}
return $rows_affected;
}
public function destroy($id)
{
$where = $this->db->quoteInto("`id` = ?", $id);
return $this->db->delete('sessions', $where);
}
public function gc($max)
{
$old = ($this->maxTime['access'] - $max);
$where = $this->db->quoteInto("`access` < ?", $old);
return $this->db->delete('sessions', $where);
}
}
?> |
Partager