diff --git a/MySqlSessionHandler.php b/MySqlSessionHandler.php index 6a9774b..d5d3194 100644 --- a/MySqlSessionHandler.php +++ b/MySqlSessionHandler.php @@ -61,10 +61,10 @@ public function setDbTable($dbTable) */ public function open() { - //delete old session handlers - $limit = time() - (3600 * 24); - $sql = sprintf("DELETE FROM %s WHERE timestamp < %s", $this->dbTable, $limit); - return $this->dbConnection->query($sql); + if (!is_a($this->dbConnection, 'mysqli')){ + throw new Exception('No session DB connection.'); + } + return true; } /** @@ -106,11 +106,14 @@ public function read($id) public function write($id, $data) { - $sql = sprintf("REPLACE INTO %s VALUES('%s', '%s', '%s')", + $sql = sprintf("REPLACE INTO %s VALUES('%s', '%s', '%s', '%s', '%s')", $this->dbTable, $this->dbConnection->escape_string($id), $this->dbConnection->escape_string($data), - time()); + time(), + $this->dbConnection->escape_string($_SERVER['REMOTE_ADDR']), + $this->dbConnection->escape_string($_SESSION['hits']) + ); return $this->dbConnection->query($sql); } @@ -137,6 +140,12 @@ public function destroy($id) */ public function gc($max) { + //Delete single use sessions (search-bots etc.) + $limit = time() - (3600 * 5); + $sql = sprintf("DELETE FROM %s WHERE hits=1 AND timestamp < %s", $this->dbTable, $limit); + $this->dbConnection->query($sql); + + //Delete according to GC $max age setting $sql = sprintf("DELETE FROM %s WHERE `timestamp` < '%s'", $this->dbTable, time() - intval($max)); return $this->dbConnection->query($sql); } diff --git a/README.md b/README.md index 1ce5e6b..ff0ddcf 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,14 @@ This class is old and I am personally not using it anymore. Maintenance is very ## Usage Create a table in your database: - CREATE TABLE `session_handler_table` ( - `id` varchar(255) NOT NULL, - `data` mediumtext NOT NULL, - `timestamp` int(255) NOT NULL, - PRIMARY KEY (`id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `sessions` ( + `id` varchar(50) NOT NULL, + `data` mediumtext NOT NULL, + `timestamp` int(255) NOT NULL, + `ip` varchar(255) NOT NULL, + `hits` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; Then have a look at [example.php](example.php).