1 | <?php |
||
41 | class DbSession extends MultiFieldSession |
||
42 | { |
||
43 | /** |
||
44 | * @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
||
45 | * After the DbSession object is created, if you want to change this property, you should only assign it |
||
46 | * with a DB connection object. |
||
47 | * Starting from version 2.0.2, this can also be a configuration array for creating the object. |
||
48 | */ |
||
49 | public $db = 'db'; |
||
50 | /** |
||
51 | * @var string the name of the DB table that stores the session data. |
||
52 | * The table should be pre-created as follows: |
||
53 | * |
||
54 | * ```sql |
||
55 | * CREATE TABLE session |
||
56 | * ( |
||
57 | * id CHAR(40) NOT NULL PRIMARY KEY, |
||
58 | * expire INTEGER, |
||
59 | * data BLOB |
||
60 | * ) |
||
61 | * ``` |
||
62 | * |
||
63 | * where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type |
||
64 | * that can be used for some popular DBMS: |
||
65 | * |
||
66 | * - MySQL: LONGBLOB |
||
67 | * - PostgreSQL: BYTEA |
||
68 | * - MSSQL: BLOB |
||
69 | * |
||
70 | * When using DbSession in a production server, we recommend you create a DB index for the 'expire' |
||
71 | * column in the session table to improve the performance. |
||
72 | * |
||
73 | * Note that according to the php.ini setting of `session.hash_function`, you may need to adjust |
||
74 | * the length of the `id` column. For example, if `session.hash_function=sha256`, you should use |
||
75 | * length 64 instead of 40. |
||
76 | */ |
||
77 | public $sessionTable = '{{%session}}'; |
||
78 | |||
79 | |||
80 | /** |
||
81 | * Initializes the DbSession component. |
||
82 | * This method will initialize the [[db]] property to make sure it refers to a valid DB connection. |
||
83 | * @throws InvalidConfigException if [[db]] is invalid. |
||
84 | */ |
||
85 | 18 | public function init() |
|
86 | { |
||
87 | 18 | parent::init(); |
|
88 | 18 | $this->db = Instance::ensure($this->db, Connection::className()); |
|
89 | 18 | } |
|
90 | |||
91 | /** |
||
92 | * Updates the current session ID with a newly generated one . |
||
93 | * Please refer to <http://php.net/session_regenerate_id> for more details. |
||
94 | * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
||
95 | */ |
||
96 | public function regenerateID($deleteOldSession = false) |
||
97 | { |
||
98 | $oldID = session_id(); |
||
99 | |||
100 | // if no session is started, there is nothing to regenerate |
||
101 | if (empty($oldID)) { |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | parent::regenerateID(false); |
||
106 | $newID = session_id(); |
||
107 | // if session id regeneration failed, no need to create/update it. |
||
108 | if (empty($newID)) { |
||
109 | Yii::warning('Failed to generate new session ID', __METHOD__); |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | $row = $this->db->useMaster(function() use ($oldID) { |
||
114 | return (new Query())->from($this->sessionTable) |
||
115 | ->where(['id' => $oldID]) |
||
116 | ->createCommand($this->db) |
||
117 | ->queryOne(); |
||
118 | }); |
||
119 | |||
120 | if ($row !== false) { |
||
121 | if ($deleteOldSession) { |
||
122 | $this->db->createCommand() |
||
123 | ->update($this->sessionTable, ['id' => $newID], ['id' => $oldID]) |
||
124 | ->execute(); |
||
125 | } else { |
||
126 | $row['id'] = $newID; |
||
127 | $this->db->createCommand() |
||
128 | ->insert($this->sessionTable, $row) |
||
129 | ->execute(); |
||
130 | } |
||
131 | } else { |
||
132 | // shouldn't reach here normally |
||
133 | $this->db->createCommand() |
||
134 | ->insert($this->sessionTable, $this->composeFields($newID, '')) |
||
135 | ->execute(); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Session read handler. |
||
141 | * @internal Do not call this method directly. |
||
142 | * @param string $id session ID |
||
143 | * @return string the session data |
||
144 | */ |
||
145 | 15 | public function readSession($id) |
|
159 | |||
160 | /** |
||
161 | * Session write handler. |
||
162 | * @internal Do not call this method directly. |
||
163 | * @param string $id session ID |
||
164 | * @param string $data session data |
||
165 | * @return bool whether session write is successful |
||
166 | */ |
||
167 | 15 | public function writeSession($id, $data) |
|
182 | |||
183 | /** |
||
184 | * Session destroy handler. |
||
185 | * @internal Do not call this method directly. |
||
186 | * @param string $id session ID |
||
187 | * @return bool whether session is destroyed successfully |
||
188 | */ |
||
189 | 6 | public function destroySession($id) |
|
197 | |||
198 | /** |
||
199 | * Session GC (garbage collection) handler. |
||
200 | * @internal Do not call this method directly. |
||
201 | * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
202 | * @return bool whether session is GCed successfully |
||
203 | */ |
||
204 | 3 | public function gcSession($maxLifetime) |
|
212 | |||
213 | /** |
||
214 | * Method typecasts $fields before passing them to PDO. |
||
215 | * Default implementation casts field `data` to `\PDO::PARAM_LOB`. |
||
216 | * You can override this method in case you need special type casting. |
||
217 | * |
||
218 | * @param array $fields Fields, that will be passed to PDO. Key - name, Value - value |
||
219 | * @return array |
||
220 | * @since 2.0.13 |
||
221 | */ |
||
222 | 15 | protected function typecastFields($fields) |
|
230 | } |
||
231 |