@@ -7,7 +7,7 @@ How to Use PdoSessionHandler to Store Sessions in the Database
7
7
The default Symfony session storage writes the session information to files.
8
8
Most medium to large websites use a database to store the session values
9
9
instead of files, because databases are easier to use and scale in a
10
- multi webserver environment.
10
+ multiple web server environment.
11
11
12
12
Symfony has a built-in solution for database session storage called
13
13
:class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Storage\\ Handler\\ PdoSessionHandler `.
@@ -172,15 +172,18 @@ of your project's data, you can use the connection settings from the
172
172
'%database_password%',
173
173
));
174
174
175
- Example SQL Statements
176
- ----------------------
175
+ .. _example-sql-statements :
176
+
177
+ Preparing the Database to Store Sessions
178
+ ----------------------------------------
179
+
180
+ Before storing sessions in the database, you must create the table that stores
181
+ the information. The following sections contain some examples of the SQL statements
182
+ you may use for your specific database engine.
177
183
178
184
MySQL
179
185
~~~~~
180
186
181
- The SQL statement for creating the needed database table might look like the
182
- following (MySQL):
183
-
184
187
.. code-block :: sql
185
188
186
189
CREATE TABLE `session` (
@@ -193,8 +196,6 @@ following (MySQL):
193
196
PostgreSQL
194
197
~~~~~~~~~~
195
198
196
- For PostgreSQL, the statement should look like this:
197
-
198
199
.. code-block :: sql
199
200
200
201
CREATE TABLE session (
@@ -207,8 +208,6 @@ For PostgreSQL, the statement should look like this:
207
208
Microsoft SQL Server
208
209
~~~~~~~~~~~~~~~~~~~~
209
210
210
- For MSSQL, the statement might look like the following:
211
-
212
211
.. code-block :: sql
213
212
214
213
CREATE TABLE [dbo].[session](
@@ -225,3 +224,16 @@ For MSSQL, the statement might look like the following:
225
224
ALLOW_PAGE_LOCKS = ON
226
225
) ON [PRIMARY]
227
226
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
227
+
228
+ .. note ::
229
+
230
+ If the session data doesn't fit in the data column, it might get truncated
231
+ by the database engine. To make matters worse, when the session data gets
232
+ corrupted, PHP ignores the data without giving a warning.
233
+
234
+ If the application stores large amounts of session data, this problem can
235
+ be solved by increasing the column size (use ``BLOB `` or even ``MEDIUMBLOB ``).
236
+ When using MySQL as the database engine, you can also enable the `strict SQL mode `_
237
+ to get noticed when such an error happens.
238
+
239
+ .. _`strict SQL mode` : https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
0 commit comments