Skip to content

[HttpFoundation] Added possibility to disable base_64 encoding of session #5483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public function __construct(\PDO $pdo, array $dbOptions = array())

$this->pdo = $pdo;
$this->dbOptions = array_merge(array(
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
'db_id_col' => 'sess_id',
'db_data_col' => 'sess_data',
'db_time_col' => 'sess_time',
'base64_encode' => true,
), $dbOptions);
}

Expand Down Expand Up @@ -141,7 +142,11 @@ public function read($id)
$sessionRows = $stmt->fetchAll(\PDO::FETCH_NUM);

if (count($sessionRows) == 1) {
return base64_decode($sessionRows[0][0]);
if ($this->dbOptions['base64_encode']) {
return base64_decode($sessionRows[0][0]);
}

return $sessionRows[0][0];
}

// session does not exist, create it
Expand All @@ -164,8 +169,12 @@ public function write($id, $data)
$dbIdCol = $this->dbOptions['db_id_col'];
$dbTimeCol = $this->dbOptions['db_time_col'];

//session data can contain non binary safe characters so we need to encode it
$encoded = base64_encode($data);
if ($this->dbOptions['base64_encode']) {
//session data can contain non binary safe characters so we need to encode it
$encoded = base64_encode($data);
} else {
$encoded = $data;
}

try {
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
Expand Down Expand Up @@ -218,8 +227,13 @@ private function createNewSession($id, $data = '')

$sql = "INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time)";

//session data can contain non binary safe characters so we need to encode it
$encoded = base64_encode($data);
if ($this->dbOptions['base64_encode']) {
//session data can contain non binary safe characters so we need to encode it
$encoded = base64_encode($data);
} else {
$encoded = $data;
}

$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ public function testSessionGC()
$storage->gc(-1);
$this->assertEquals(0, count($this->pdo->query('SELECT * FROM sessions')->fetchAll()));
}

public function testEncoding()
{
$storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions'), array());

$storage->write('foo', 'bar');
$result = $this->pdo->query('SELECT * FROM sessions')->fetchAll();
$this->assertEquals(base64_encode('bar'), $result[0]['sess_data']);

$storage = new PdoSessionHandler($this->pdo, array('db_table' => 'sessions', 'base64_encode' => false), array());

$storage->write('foo', 'bar');
$result = $this->pdo->query('SELECT * FROM sessions')->fetchAll();
$this->assertEquals('bar', $result[0]['sess_data']);
}
}