title: Session is destroyed when browser is closed. | ZF2
body: <p>I want to implement session and cookies in my zend application. Session is working fine within the tabs but when I reopen the browser then sessionId in the cookie changes and my application shows user is not logged in.</p>
  
  <p>Below is the code of Module.php of Application Module.</p>
  
  <pre><code>public function onBootstrap(MvcEvent $e)
      {
          $eventManager        = $e-&gt;getApplication()-&gt;getEventManager();
          $moduleRouteListener = new ModuleRouteListener();
          $moduleRouteListener-&gt;attach($eventManager);
          $eventManager-&gt;attach('dispatch', array($this, 'loadConfiguration' ));
  
          $this-&gt;initSession(array(
              'remember_me_seconds' =&gt; 864000,                  
              'use_cookies' =&gt; true,
              'cookie_httponly' =&gt; true,  
              'cookie_secure' =&gt; false,       
          ));
      } 
  
  public function initSession($config){
          $sessionConfig = new SessionConfig();
          $sessionConfig-&gt;setOptions($config);
          $sessionManager = new SessionManager($sessionConfig);
          $sessionManager-&gt;start();
          Container::setDefaultManager($sessionManager);
  
      }
  
  public function loadConfiguration(MvcEvent $e)
      {
          $controller = $e-&gt;getTarget();
          $session = new Container('user_session_data');
  
          if(isset($session-&gt;user_id)){
              //user is logged in set his data to viewModel
              $controller-&gt;layout()-&gt;user_id = $session-&gt;user_id;
              $controller-&gt;layout()-&gt;firstName = $session-&gt;firstName;
              $controller-&gt;layout()-&gt;lastName = $session-&gt;lastName;
              $controller-&gt;layout()-&gt;profilePicture = $session-&gt;profilePicture;
          }
      } 
  </code></pre>
  
  <p>I access these values in my layout.phtml file. Below is the code to save session data when user is successfully logged in.</p>
  
  <pre><code>$session = new Container('user_session_data');
              if($session-&gt;user_id){
                  echo "You are already logged in";
              }
              // store user details in session
              $session-&gt;user_id = $responseDataArray['data']['id'];
              $session-&gt;firstName = $responseDataArray['data']['firstName'];
              $session-&gt;lastName = $responseDataArray['data']['lastName'];
              $session-&gt;profilePicture = $responseDataArray['data' ['profilePicture']; 
  </code></pre>
  
  <p>I don't know what I am missing to persist session data.</p>