From 5e9b5abc956c594204df782010c9f0ffe971ffc5 Mon Sep 17 00:00:00 2001 From: pzinovkin Date: Wed, 28 Apr 2010 19:52:20 +0000 Subject: [PATCH] Storing session in database, #16 git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@146 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- session/Session.model.php | 70 +++++++++++++++++++++++++++++++++++++++++++++++ session/Session.php | 13 ++++++++- session/session.sql | 10 +++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 session/Session.model.php create mode 100644 session/session.sql diff --git a/session/Session.model.php b/session/Session.model.php new file mode 100644 index 0000000..091aa6a --- /dev/null +++ b/session/Session.model.php @@ -0,0 +1,70 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Session + * @since 2010-02-28 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class SessionModel extends Model +{ + + protected $life_time; + + public function __construct() + { + parent::__construct(); + $this->life_time = get_cfg_var('session.gc_maxlifetime'); + } + + /* Session handler methods */ + + public function open($save_path, $sess_name) + { + return true; + } + + public function close() + { + return true; + } + + public function read($id) + { + $sql = 'SELECT `data` FROM ' . $this->table() . ' WHERE `id`=? AND `expires` > UNIX_TIMESTAMP()'; + return (string) $this->fetchField($sql, $id, 'data'); + } + + public function write($id, $data) + { + preg_match('/user\|.+s:2:"id";s:(\d+):"(\d+)"/', $data, $match); + $user_id = empty($match) ? 0 : (int) $match[2]; + + $sql = 'INSERT INTO ' . $this->table() . ' (`id`, `expires`, `user_id`, `data`)' + . ' VALUES(' . $this->quote($id) . ', UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', ' . $user_id . ', ' . $this->quote($data) . ')' + . ' ON DUPLICATE KEY UPDATE `expires`=UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', `user_id`=' . $user_id . ', `data`=' . $this->quote($data); + $affected = $this->db->query($sql)->affectedRows(); + return (bool) ($this->getInsertId()) ? $this->getInsertId() : $affected; + } + + public function destroy($id) + { + return (bool) $this->db->delete($this->table(false), array('`id`=?' => (string) $id)); + } + + public function gc($max_life_time) + { + return (bool) $this->db->delete($this->table(false), '`expires` < UNIX_TIMESTAMP()'); + } + + /* End of Session handler methods */ + + + public function destroyByUserId($user_id) + { + return $this->db->delete($this->table(false), array('`user_id`=?' => $user_id)); + } +} \ No newline at end of file diff --git a/session/Session.php b/session/Session.php index 96546b2..b2546fd 100644 --- a/session/Session.php +++ b/session/Session.php @@ -171,7 +171,6 @@ class Session } self::$started = true; session_start(); - self::regenerateId(); } /** @@ -220,4 +219,16 @@ class Session ); } } + + public static function setSessionHandler($handler) + { + session_set_save_handler( + array($handler, 'open'), + array($handler, 'close'), + array($handler, 'read'), + array($handler, 'write'), + array($handler, 'destroy'), + array($handler, 'gc') + ); + } } diff --git a/session/session.sql b/session/session.sql new file mode 100644 index 0000000..c49b7a8 --- /dev/null +++ b/session/session.sql @@ -0,0 +1,10 @@ + +CREATE TABLE IF NOT EXISTS `session` ( + `id` char(32) NOT NULL, + `expires` int(11) unsigned NOT NULL, + `user_id` int(11) unsigned DEFAULT '0', + `data` text NOT NULL, + PRIMARY KEY (`id`), + KEY `expires` (`expires`), + KEY `user_id` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file