Skip to content

Commit

Permalink
Prevent session data from being re-persisted if closed or destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
Caffe1neAdd1ct authored and sergeyklay committed Nov 6, 2018
1 parent ce8e8ec commit 90d9f50
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions Library/Phalcon/Session/Adapter/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public function __construct($options = null)
*/
public function open()
{
return true;
$this->_started = true;
return $this->isStarted();
}

/**
Expand All @@ -97,7 +98,8 @@ public function open()
*/
public function close()
{
return false;
$this->_started = false;
return $this->isStarted();
}

/**
Expand All @@ -109,7 +111,11 @@ public function close()
public function read($sessionId)
{
$maxLifetime = (int) ini_get('session.gc_maxlifetime');


if (!$this->isStarted()) {
return false;
}

$options = $this->getOptions();
$row = $this->connection->fetchOne(
sprintf(
Expand Down Expand Up @@ -152,7 +158,7 @@ public function write($sessionId, $data)
Db::FETCH_NUM,
[$sessionId]
);

if ($row[0] > 0) {
return $this->connection->execute(
sprintf(
Expand All @@ -164,19 +170,23 @@ public function write($sessionId, $data)
),
[$data, time(), $sessionId]
);
} else {
return $this->connection->execute(
sprintf(
'INSERT INTO %s (%s, %s, %s, %s) VALUES (?, ?, ?, NULL)',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_session_id']),
$this->connection->escapeIdentifier($options['column_data']),
$this->connection->escapeIdentifier($options['column_created_at']),
$this->connection->escapeIdentifier($options['column_modified_at'])
),
[$sessionId, $data, time()]
);
}

if (!$this->isStarted()) {
return false;
}

return $this->connection->execute(
sprintf(
'INSERT INTO %s (%s, %s, %s, %s) VALUES (?, ?, ?, NULL)',
$this->connection->escapeIdentifier($options['table']),
$this->connection->escapeIdentifier($options['column_session_id']),
$this->connection->escapeIdentifier($options['column_data']),
$this->connection->escapeIdentifier($options['column_created_at']),
$this->connection->escapeIdentifier($options['column_modified_at'])
),
[$sessionId, $data, time()]
);
}

/**
Expand Down

0 comments on commit 90d9f50

Please sign in to comment.