简体   繁体   中英

Symfony PdoSessionHandler not working as expected with PostgreSql

Im trying to move php sessions from files to database. Following this: pdo_session_storage I set up symfony as following:

config.yml:

session:
    handler_id:  session.handler.pdo

services.yml:

session.handler.pdo:
    class:  Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
    arguments:
        - "pgsql:host=%database_host_access%;port=%database_port_access%;dbname=%database_name_access%"
        - { db_username: %database_user_access%, db_password: %database_password_access% }

The database is correctly seen and for each session a row is created in table: sessions .

The problem is: every time I reload a page the sessions attributes get lost. It seems like data are written from zero instead of being appended to the existing attributes.

With some debugging I found out that NativeSessionStorage->loadSession() load data from $_SESSION which is empty so SessionHandlerProxy->write() writes data with empty attributes.

NativeSessionStorage::loadSession():

protected function loadSession(array &$session = null)
{
    if (null === $session) {
        $session = &$_SESSION;
    }

    $bags = array_merge($this->bags, array($this->metadataBag));

    foreach ($bags as $bag) {
        $key = $bag->getStorageKey();
        $session[$key] = isset($session[$key]) ? $session[$key] : array();
        $bag->initialize($session[$key]);
    }

    $this->started = true;
    $this->closed = false;
}

Where &$_SESSION; is always empty.

This behavior is not present if I use mysql as session.handler.pdo argument or let php manages sessions via files.

I can't find resources about this topic that in my opinion is not a minor problem, did someone have some suggestion?

Im using Postgres version 9.4.1.0 and Symfony version 2.6.5.

I found the problem: PostgreSql version 9.0 changes the default bytea output in hex format but Symfony handles resources in escape .

A temporary solution could be: ALTER DATABASE dbname SET bytea_output = 'escape';

Here is the git/symfony issue: https://github.com/symfony/symfony/issues/14569

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM