Talk To Us For FREE

We are in the Business of Support and that starts before you even become a client.


  • Please prove you are human by selecting the Cup.

Symfony2 Store Sessions in the Database

Categories : Article, Symfony

With Symfony 2 development you have a built in solution for storing sessions in the database. This is called PdoSessionHandler. In order to use this option in Symfony 2, you need to change a few parameters here and there. The config.yml file is a good start.


# 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%"]

Here are the details:
db_table is the name of the session table in the database
db_id_col is the name of the id column in your session table (VARCHAR(255) or larger)
db_data_col is the name of the value column in your session table (TEXT or CLOB)
db_time_col is the name of the time column in your session table (INTEGER)

Sharing Database Connection Information
The database connection settings in the given example are defines for session storage connections only. This works fine if you use separate database for session data. If you require to store the session data in the same database as your project data, the parameters.yml file needs to be tweaked. By referencing the database related parameters.

pdo:
    class: PDO
    arguments:
        - "mysql:host=%database_host%;port=%database_port%;dbname=%database_name%"
        - "%database_user%"
        - "%database_password%"

Symfony 2 defaults to store the session information in files. But many medium or large websites prefer to store this information to databases. Databases can easily scale in a multi website environment. These were a few tips to configure the session information to be stored in the database instead of files.

[Total: 0    Average: 0/5]

Client Saying

If you're starting a new business or want to upgrade an existing site, I strongly recommend…
Mr. Manjesh Singh
CTO
Daniel Coburn, the president of Bragshare.com gives the honest opinion regarding his experience working with Brainvire…
Mr. Daniel Coburn
Chad Gundry, the CEO of Bragshare.com says that he has worked extensively with Brainvire team since…
Mr. Chad Gundry
The best thing about Brainvire is Compatibility. Whenever we have a query Brainvire is always ready…
Mr Craig Agranoff
Brainvire is a professional company with professional developers who understand the problems that we are facing…
Mr. Clarke Verdel

Latest Happenings

  • 17 Mar 2017
    Brainvire - Walmart Collaboration Hits Another Retail Height [ + ]
  • 03 Mar 2017
    Brainvire’s Vision for Becoming the Global Mobile Dominant Strengthens with MWC17 [ + ]
Enquiry