ZF3 Session empty on each page load

308 Views Asked by At

In ZF3, I'm initializing my session as following:

config/autoload/global.php

'session_config' => [
    'name' => 'gintra3',
    'cookie_lifetime' => 60*60*1,
    'gc_maxlifetime'     => 60*60*24*30,
],
'session_storage' => [
    'type' => Zend\Session\Storage\ArrayStorage::class,
],
'session_manager' => [
    'storage' => Zend\Session\Storage\SessionArrayStorage::class,
    'validators' => [
        Zend\Session\Validator\RemoteAddr::class,
        Zend\Session\Validator\HttpUserAgent::class,
    ],
],

module/Application/Module.php

$sessionManager = $e->getApplication()->getServiceManager()->get(SessionManager::class);
$sessionTableGateway = new TableGateway('session',$e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter'));
$sessionSaveHandler = new DbTableGateway($sessionTableGateway,new DbTableGatewayOptions());
$sessionManager->setSaveHandler($sessionSaveHandler);

Now, when I'm storing data like this on page 1:

$sessionC = new Container('test');
$sessionC->testVariable = "helloWorld";

And retrieve it on page 2 like this:

$sessionC = new Container('test');
Debug::dump($sessionC->testVariable);

I get an output on page 2 like this:

vendor/zendframework/zend-debug/src/Debug.php:97:null

When I check the database table, the 'data' column of the corresponding session_id contains "helloWorld" after loading page 1. After loading page 2, it does not contain "helloWorld" anymore.

If I comment out all session-related lines in Module.php, a PHPSESSID cookie is created on page load and session data saving and storing works fine now.

It does not matter if I use DbTableGateway as a save_handler or just use standard save_handler. The problem occurs in both cases.

When using my individual session storage, also FlashMessenger and Csrf-Form-Validation stop working due to the same mechanism of loosing all session data on page load.

Where is my problem? What am I overseeing? Thanks a lot for some ideas.

0

There are 0 best solutions below