.. index::
single: Session; Database Storage
How to use PdoSessionHandler to store Sessions in the Database
==============================================================
The default session storage of Symfony2 writes the session information to
file(s). Most medium to large websites use a database to store the session
values instead of files, because databases are easier to use and scale in a
multi-webserver environment.
Symfony2 has a built-in solution for database session storage called
:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\PdoSessionHandler`.
To use it, you just need to change some parameters in ``config.yml`` (or the
configuration format of your choice):
.. configuration-block::
.. code-block:: yaml
# app/config/config.yml
framework:
session:
# ...
handler_id: session.handler.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
services:
pdo:
class: PDO
arguments:
dsn: "mysql:dbname=mydatabase"
user: myuser
password: mypassword
calls:
- [setAttribute, [3, 2]] # \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION
session.handler.pdo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
arguments: ["@pdo", "%pdo.db_options%"]
.. code-block:: xml
session
session_id
session_value
session_time
mysql:dbname=mydatabase
myuser
mypassword
PDO::ATTR_ERRMODE
PDO::ERRMODE_EXCEPTION
%pdo.db_options%
.. code-block:: php
// app/config/config.php
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
$container->loadFromExtension('framework', array(
...,
'session' => array(
// ...,
'handler_id' => 'session.handler.pdo',
),
));
$container->setParameter('pdo.db_options', array(
'db_table' => 'session',
'db_id_col' => 'session_id',
'db_data_col' => 'session_value',
'db_time_col' => 'session_time',
));
$pdoDefinition = new Definition('PDO', array(
'mysql:dbname=mydatabase',
'myuser',
'mypassword',
));
$pdoDefinition->addMethodCall('setAttribute', array(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION));
$container->setDefinition('pdo', $pdoDefinition);
$storageDefinition = new Definition('Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler', array(
new Reference('pdo'),
'%pdo.db_options%',
));
$container->setDefinition('session.handler.pdo', $storageDefinition);
* ``db_table``: The name of the session table in your database
* ``db_id_col``: The name of the id column in your session table (VARCHAR(255) or larger)
* ``db_data_col``: The name of the value column in your session table (TEXT or CLOB)
* ``db_time_col``: The name of the time column in your session table (INTEGER)
Sharing your Database Connection Information
--------------------------------------------
With the given configuration, the database connection settings are defined for
the session storage connection only. This is OK when you use a separate
database for the session data.
But if you'd like to store the session data in the same database as the rest
of your project's data, you can use the connection settings from the
``parameters.yml`` file by referencing the database-related parameters defined there:
.. configuration-block::
.. code-block:: yaml
pdo:
class: PDO
arguments:
- "mysql:host=%database_host%;port=%database_port%;dbname=%database_name%"
- "%database_user%"
- "%database_password%"
.. code-block:: xml
mysql:host=%database_host%;port=%database_port%;dbname=%database_name%
%database_user%
%database_password%
.. code-block:: php
$pdoDefinition = new Definition('PDO', array(
'mysql:host=%database_host%;port=%database_port%;dbname=%database_name%',
'%database_user%',
'%database_password%',
));
Example SQL Statements
----------------------
MySQL
~~~~~
The SQL statement for creating the needed database table might look like the
following (MySQL):
.. code-block:: sql
CREATE TABLE `session` (
`session_id` varchar(255) NOT NULL,
`session_value` text NOT NULL,
`session_time` int(11) NOT NULL,
PRIMARY KEY (`session_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
PostgreSQL
~~~~~~~~~~
For PostgreSQL, the statement should look like this:
.. code-block:: sql
CREATE TABLE session (
session_id character varying(255) NOT NULL,
session_value text NOT NULL,
session_time integer NOT NULL,
CONSTRAINT session_pkey PRIMARY KEY (session_id)
);
Microsoft SQL Server
~~~~~~~~~~~~~~~~~~~~
For MSSQL, the statement might look like the following:
.. code-block:: sql
CREATE TABLE [dbo].[session](
[session_id] [nvarchar](255) NOT NULL,
[session_value] [ntext] NOT NULL,
[session_time] [int] NOT NULL,
PRIMARY KEY CLUSTERED(
[session_id] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]