Skip to content

Commit

Permalink
Use timestamp types for storing session timestamps
Browse files Browse the repository at this point in the history
They are easier to work with than plain integers.
  • Loading branch information
pstef committed Sep 22, 2019
1 parent 4b1774a commit 529b5ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions system/Session/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function write($sessionID, $sessionData): bool
$insertData = [
'id' => $sessionID,
'ip_address' => $this->ipAddress,
'timestamp' => time(),
'timestamp' => 'now()',
'data' => $this->platform === 'postgre' ? '\x' . bin2hex($sessionData) : $sessionData,
];

Expand All @@ -262,7 +262,7 @@ public function write($sessionID, $sessionData): bool
}

$updateData = [
'timestamp' => time(),
'timestamp' => 'now()',
];

if ($this->fingerprint !== md5($sessionData))
Expand Down Expand Up @@ -345,7 +345,8 @@ public function destroy($sessionID): bool
*/
public function gc($maxlifetime): bool
{
return ($this->db->table($this->table)->delete('timestamp < ' . (time() - $maxlifetime))) ? true : $this->fail();
$interval = implode(" '"[(int)($this->platform === 'postgre')], ['', "{$maxlifetime} second", '']);
return ($this->db->table($this->table)->delete("timestamp < now() - INTERVAL {$interval}")) ? true : $this->fail();
}

//--------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/libraries/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ For MySQL::
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`timestamp` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);
Expand All @@ -595,7 +595,7 @@ For PostgreSQL::
CREATE TABLE "ci_sessions" (
"id" varchar(128) NOT NULL,
"ip_address" varchar(45) NOT NULL,
"timestamp" bigint DEFAULT 0 NOT NULL,
"timestamp" timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
"data" bytea DEFAULT '' NOT NULL
);

Expand Down

0 comments on commit 529b5ad

Please sign in to comment.