浏览器关闭时会破坏会话。 | ZF2

时间:2016-05-30 16:38:43

标签: php session cookies zend-framework zend-framework2

我想在我的zend应用程序中实现会话和cookie。会话在选项卡中运行良好,但是当我重新打开浏览器时,cookie中的sessionId发生了变化,我的应用程序显示用户未登录。

以下是应用程序模块的Module.php的代码。

public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $eventManager->attach('dispatch', array($this, 'loadConfiguration' ));

        $this->initSession(array(
            'remember_me_seconds' => 864000,                  
            'use_cookies' => true,
            'cookie_httponly' => true,  
            'cookie_secure' => false,       
        ));
    } 

public function initSession($config){
        $sessionConfig = new SessionConfig();
        $sessionConfig->setOptions($config);
        $sessionManager = new SessionManager($sessionConfig);
        $sessionManager->start();
        Container::setDefaultManager($sessionManager);

    }

public function loadConfiguration(MvcEvent $e)
    {
        $controller = $e->getTarget();
        $session = new Container('user_session_data');

        if(isset($session->user_id)){
            //user is logged in set his data to viewModel
            $controller->layout()->user_id = $session->user_id;
            $controller->layout()->firstName = $session->firstName;
            $controller->layout()->lastName = $session->lastName;
            $controller->layout()->profilePicture = $session->profilePicture;
        }
    } 

我在layout.phtml文件中访问这些值。以下是用户成功登录时保存会话数据的代码。

$session = new Container('user_session_data');
            if($session->user_id){
                echo "You are already logged in";
            }
            // store user details in session
            $session->user_id = $responseDataArray['data']['id'];
            $session->firstName = $responseDataArray['data']['firstName'];
            $session->lastName = $responseDataArray['data']['lastName'];
            $session->profilePicture = $responseDataArray['data' ['profilePicture']; 

我不知道我缺少什么来坚持会话数据。

0 个答案:

没有答案