4

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.

1 Answer 1

6

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 is: ALTER DATABASE dbname SET bytea_output = 'escape';

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this answer! I couldn’t figure out, why my stuff was working on dev, but not on production. Adding the above line in dev resulted in Migration ... was executed but did not result in any SQL statements.. I deployed with that migration anyway, and the production Postgres apparently had it in hex, because that line DID result in the SQL statement in production and now it works!! I work with Postgres 9.5.6 in dev, 9.6.3 in production, so I didn't think it would have been different. Production was AWS ElasticBeanstalk with RDS Postgres.
this is still an issue with Symfony 4.2 and AWS Psql 9.6.9
this is still an issue with Symfony 5.1.6 and PostgreSQL 12.4. Thanks for the fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.