Skip to content

Commit

Permalink
Use Postgres type BYTEA to store session data
Browse files Browse the repository at this point in the history
It's not so difficult to use after all and the type saves space
compared to base64-encoded text.
  • Loading branch information
pstef committed Sep 22, 2019
1 parent 6fc1af2 commit 4b1774a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
9 changes: 3 additions & 6 deletions system/Session/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function read($sessionID): string
$this->sessionID = $sessionID;

$builder = $this->db->table($this->table)
->select('data')
->select($this->platform === 'postgre' ? "encode(data, 'base64') AS data" : 'data')
->where('id', $sessionID);

if ($this->matchIP)
Expand All @@ -188,9 +188,6 @@ public function read($sessionID): string
return '';
}

// PostgreSQL's variant of a BLOB datatype is Bytea, which is a
// PITA to work with, so we use base64-encoded data in a TEXT
// field instead.
if (is_bool($result))
{
$result = '';
Expand Down Expand Up @@ -243,7 +240,7 @@ public function write($sessionID, $sessionData): bool
'id' => $sessionID,
'ip_address' => $this->ipAddress,
'timestamp' => time(),
'data' => $this->platform === 'postgre' ? base64_encode($sessionData) : $sessionData,
'data' => $this->platform === 'postgre' ? '\x' . bin2hex($sessionData) : $sessionData,
];

if (! $this->db->table($this->table)->insert($insertData))
Expand All @@ -270,7 +267,7 @@ public function write($sessionID, $sessionData): bool

if ($this->fingerprint !== md5($sessionData))
{
$updateData['data'] = ($this->platform === 'postgre') ? base64_encode($sessionData) : $sessionData;
$updateData['data'] = ($this->platform === 'postgre') ? '\x' . bin2hex($sessionData) : $sessionData;
}

if (! $builder->update($updateData))
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ For PostgreSQL::
"id" varchar(128) NOT NULL,
"ip_address" varchar(45) NOT NULL,
"timestamp" bigint DEFAULT 0 NOT NULL,
"data" text DEFAULT '' NOT NULL
"data" bytea DEFAULT '' NOT NULL
);

CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");
Expand Down

0 comments on commit 4b1774a

Please sign in to comment.