zend-framework2zend-session

How to change session cookie path in ZF2?


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();


Solution

  • 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);