19
19
* This means requests for the same session will wait until the other one finished.
20
20
* PHPs internal files session handler also works this way.
21
21
*
22
- * Session data is a binary string that can contain non-printable characters like the null byte.
23
- * For this reason this handler base64 encodes the data to be able to save it in a character column.
24
- *
25
22
* Attention: Since SQLite does not support row level locks but locks the whole database,
26
23
* it means only one session can be accessed at a time. Even different sessions would wait
27
24
* for another to finish. So saving session in SQLite should only be considered for
28
25
* development or prototypes.
29
26
*
27
+ * Session data is a binary string that can contain non-printable characters like the null byte.
28
+ * For this reason it must be saved in a binary column in the database like BLOB in MySQL.
29
+ * Saving it in a character column could corrupt the data.
30
+ *
30
31
* @see http://php.net/sessionhandlerinterface
31
32
*
32
33
* @author Fabien Potencier <[email protected] >
@@ -145,11 +146,7 @@ public function read($sessionId)
145
146
// We use fetchAll instead of fetchColumn to make sure the DB cursor gets closed
146
147
$ sessionRows = $ stmt ->fetchAll (\PDO ::FETCH_NUM );
147
148
148
- if ($ sessionRows ) {
149
- return base64_decode ($ sessionRows [0 ][0 ]);
150
- }
151
-
152
- return '' ;
149
+ return $ sessionRows ? $ sessionRows [0 ][0 ] : '' ;
153
150
} catch (\PDOException $ e ) {
154
151
$ this ->rollback ();
155
152
@@ -195,8 +192,6 @@ public function destroy($sessionId)
195
192
*/
196
193
public function write ($ sessionId , $ data )
197
194
{
198
- $ encoded = base64_encode ($ data );
199
-
200
195
// The session ID can be different from the one previously received in read()
201
196
// when the session ID changed due to session_regenerate_id(). So we have to
202
197
// do an insert or update even if we created a row in read() for locking.
@@ -208,7 +203,7 @@ public function write($sessionId, $data)
208
203
if (null !== $ mergeSql ) {
209
204
$ mergeStmt = $ this ->pdo ->prepare ($ mergeSql );
210
205
$ mergeStmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
211
- $ mergeStmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
206
+ $ mergeStmt ->bindParam (':data ' , $ data , \PDO ::PARAM_LOB );
212
207
$ mergeStmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
213
208
$ mergeStmt ->execute ();
214
209
@@ -219,7 +214,7 @@ public function write($sessionId, $data)
219
214
"UPDATE $ this ->table SET $ this ->dataCol = :data, $ this ->timeCol = :time WHERE $ this ->idCol = :id "
220
215
);
221
216
$ updateStmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
222
- $ updateStmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
217
+ $ updateStmt ->bindParam (':data ' , $ data , \PDO ::PARAM_LOB );
223
218
$ updateStmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
224
219
$ updateStmt ->execute ();
225
220
@@ -236,7 +231,7 @@ public function write($sessionId, $data)
236
231
"INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) "
237
232
);
238
233
$ insertStmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
239
- $ insertStmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
234
+ $ insertStmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_LOB );
240
235
$ insertStmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
241
236
$ insertStmt ->execute ();
242
237
} catch (\PDOException $ e ) {
0 commit comments