Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions MySqlSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).<br>
Expand Down