From 117943f524717456ea9f8c5d655543e5e62cf1b1 Mon Sep 17 00:00:00 2001 From: pzinovkin Date: Thu, 20 May 2010 07:59:57 +0000 Subject: [PATCH] ip storing in session, #13 git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@147 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- model/DbDriver.php | 2 +- session/Session.model.php | 8 +++++--- session/session.sql | 24 +++++++++++++++--------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/model/DbDriver.php b/model/DbDriver.php index 5b878e6..444d2af 100644 --- a/model/DbDriver.php +++ b/model/DbDriver.php @@ -173,7 +173,7 @@ abstract class DbDriver public function quoteInto($text, $value) { - $pos = strpos($text, '?'); + $pos = mb_strpos($text, '?'); if ($pos === false) { return $text; } diff --git a/session/Session.model.php b/session/Session.model.php index 091aa6a..fd54b84 100644 --- a/session/Session.model.php +++ b/session/Session.model.php @@ -43,9 +43,11 @@ class SessionModel extends Model 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); + $ip = Env::Server('HTTP_X_FORWARDED_FOR', Env::Server('REMOTE_ADDR')); + + $sql = 'INSERT INTO ' . $this->table() . ' (`id`, `expires`, `ip`, `user_id`, `data`)' + . ' VALUES(' . $this->quote($id) . ', UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', INET_ATON(' . $this->quote($ip) . '), ' . $user_id . ', ' . $this->quote($data) . ')' + . ' ON DUPLICATE KEY UPDATE `expires`=UNIX_TIMESTAMP() + ' . (int) $this->life_time . ', `ip`=INET_ATON(' . $this->quote($ip) . '), `user_id`=' . $user_id . ', `data`=' . $this->quote($data); $affected = $this->db->query($sql)->affectedRows(); return (bool) ($this->getInsertId()) ? $this->getInsertId() : $affected; } diff --git a/session/session.sql b/session/session.sql index c49b7a8..dd21904 100644 --- a/session/session.sql +++ b/session/session.sql @@ -1,10 +1,16 @@ +-- +-- $Id$ +-- -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 +DROP TABLE IF EXISTS `session`; + +CREATE TABLE `session` ( + `id` CHAR( 32 ) NOT NULL , + `expires` INT UNSIGNED NOT NULL , + `ip` INT UNSIGNED NOT NULL , + `user_id` INT UNSIGNED NOT NULL DEFAULT 0, + `data` TEXT NOT NULL , + PRIMARY KEY ( `id` ) , + INDEX `expires` ( `expires` ) , + INDEX `user_id` ( `user_id` ) +) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8; \ No newline at end of file