How to change session cookie path in ZF2?

Tag: zend-framework2 , zend-session Author: zx3508111 Date: 2013-04-17

I want to change session cookie path in ZF2. By default it is / I want to change it on /my_path/ I tried to do it with SessionManager

$config = new \Zend\Session\Config\StandardConfig();
$config->setOptions(array(
    'cookie_path' => '/my_path/',
));
$sessionManager = new \Zend\Session\SessionManager($config);
\Zend\Session\Container::setDefaultManager($sessionManager);
$sessionManager->start();

But it does not work


Resolve: change first line on $config = new \Zend\Session\Config\SessionConfig();

Best Answer

It's not enough to just create the session manager, you also need to tell the session containers about it, the Container class has a static method to do that. Add the following line to your code

    \Zend\Session\Container::setDefaultManager($sessionManager);

comments:

I tried this (updated question), it does not work
Are you actually starting the session after that using $sessionManager->start();
Yes I do. (updated question again)
If I change first line on $config = new \Zend\Session\Config\SessionConfig(); then it works fine