20
20
class PdoSessionHandler implements \SessionHandlerInterface
21
21
{
22
22
/**
23
- * PDO instance.
24
- *
25
- * @var \PDO
23
+ * @var \PDO PDO instance.
26
24
*/
27
25
private $ pdo ;
28
26
29
27
/**
30
- * Database options.
31
- *
32
- *
33
- * @var array
28
+ * @var array Database options.
34
29
*/
35
30
private $ dbOptions ;
36
31
37
32
/**
38
33
* Constructor.
39
34
*
35
+ * List of available options:
36
+ * * db_table: The name of the table [required]
37
+ * * db_id_col: The column where to store the session id [default: sess_id]
38
+ * * db_data_col: The column where to store the session data [default: sess_data]
39
+ * * db_time_col: The column where to store the timestamp [default: sess_time]
40
+ *
40
41
* @param \PDO $pdo A \PDO instance
41
42
* @param array $dbOptions An associative array of DB options
42
43
*
@@ -166,20 +167,20 @@ public function write($id, $data)
166
167
//session data can contain non binary safe characters so we need to encode it
167
168
$ encoded = base64_encode ($ data );
168
169
169
- if ( ' mysql ' === $ this -> pdo -> getAttribute (\ PDO :: ATTR_DRIVER_NAME )) {
170
- // MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
171
- // it could result in calling createNewSession () whereas the session already exists in
172
- // the DB which would fail as the id is unique
173
- $ stmt = $ this -> pdo -> prepare (
174
- " INSERT INTO $ dbTable ( $ dbIdCol , $ dbDataCol , $ dbTimeCol ) VALUES (:id, :data, :time) " .
175
- " ON DUPLICATE KEY UPDATE $ dbDataCol = VALUES( $ dbDataCol) , $ dbTimeCol = VALUES( $ dbTimeCol ) "
176
- );
177
- $ stmt -> bindParam ( ' :id ' , $ id , \ PDO :: PARAM_STR );
178
- $ stmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
179
- $ stmt ->bindValue (':time ' , time () , \PDO ::PARAM_INT );
180
- $ stmt ->execute ( );
181
- } else {
182
- try {
170
+ try {
171
+ if ( ' mysql ' === $ this -> pdo -> getAttribute (\ PDO :: ATTR_DRIVER_NAME )) {
172
+ // MySQL would report $stmt->rowCount () = 0 on UPDATE when the data is left unchanged
173
+ // it could result in calling createNewSession() whereas the session already exists in
174
+ // the DB which would fail as the id is unique
175
+ $ stmt = $ this -> pdo -> prepare (
176
+ " INSERT INTO $ dbTable ( $ dbIdCol , $ dbDataCol, $ dbTimeCol) VALUES (:id, :data, :time) " .
177
+ " ON DUPLICATE KEY UPDATE $ dbDataCol = VALUES( $ dbDataCol ), $ dbTimeCol = VALUES( $ dbTimeCol ) "
178
+ );
179
+ $ stmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
180
+ $ stmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
181
+ $ stmt ->bindValue ( ' :time ' , time (), \ PDO :: PARAM_INT );
182
+ $ stmt -> execute ();
183
+ } else {
183
184
$ stmt = $ this ->pdo ->prepare ("UPDATE $ dbTable SET $ dbDataCol = :data, $ dbTimeCol = :time WHERE $ dbIdCol = :id " );
184
185
$ stmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
185
186
$ stmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
@@ -191,9 +192,9 @@ public function write($id, $data)
191
192
// session_regenerate_id()
192
193
$ this ->createNewSession ($ id , $ data );
193
194
}
194
- } catch (\PDOException $ e ) {
195
- throw new \RuntimeException (sprintf ('PDOException was thrown when trying to write the session data: %s ' , $ e ->getMessage ()), 0 , $ e );
196
195
}
196
+ } catch (\PDOException $ e ) {
197
+ throw new \RuntimeException (sprintf ('PDOException was thrown when trying to write the session data: %s ' , $ e ->getMessage ()), 0 , $ e );
197
198
}
198
199
199
200
return true ;
0 commit comments