首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend/ ZfcUser会话

Zend/ ZfcUser会话
EN

Stack Overflow用户
提问于 2014-02-23 19:56:27
回答 3查看 2.1K关注 0票数 0

我在我的应用中使用了ZfcUser,我需要控制超时参数。因为它不是配置的一部分,所以我想在引导时将我自己的Zend/Session对象(使用remember_me_seconds参数)设置为ZfcUser,但我不知道如何设置。

碰巧,已经有人这么做了吗?

EN

回答 3

Stack Overflow用户

发布于 2015-09-14 16:49:31

设置会话参数的最简单方法如下:config\autoload\global.php

代码语言:javascript
复制
return array(
    'service_manager' => [
        'factories' => [
            // Configures the default SessionManager instance
            'Zend\Session\ManagerInterface' => 'Zend\Session\Service\SessionManagerFactory',
            // Provides session configuration to SessionManagerFactory
            'Zend\Session\Config\ConfigInterface' => 'Zend\Session\Service\SessionConfigFactory',
        ],
    ],
    'session_manager' => [
        // SessionManager config: validators, etc
    ],
    'session_config' => [
        'cache_expire' => 86400,
        'cookie_lifetime' => 86400,
        'remember_me_seconds' => 86400,
        'gc_probability' => 10,
        'gc_divisor' => 1000,
        'use_cookies' => true,
        'cookie_httponly' => true,
        'cookie_lifetime' => 0, // to reset lifetime to maximum at every click
        'gc_maxlifetime' => 86400,
    ],
);

并将行添加到Module.phponBootstrap方法中

代码语言:javascript
复制
public function onBootstrap(MvcEvent $e)
{
    $manager = $e->getApplication()->getServiceManager()->get('Zend\Session\ManagerInterface');
}

这会影响会话设置,phpinfo()会显示出来。

我在zfcuser github里找到的

票数 1
EN

Stack Overflow用户

发布于 2014-02-23 20:37:05

我不使用zfcuser,但试试这个

autoload/global.php

代码语言:javascript
复制
<?php

use Zend\Session\Config\SessionConfig;
use Zend\Session\SessionManager;
use Zend\Session\Container;

return array(
    'service_manager' => array(
        'factories' => array(
            'SessionManager' => function($sm) {
                $sessionConfig = new SessionConfig();
                $sessionConfig->setOption('remember_me_seconds', 1440);

                $sessionManager = new SessionManager($sessionConfig);
                Container::setDefaultManager($sessionManager);

                return $sessionManager;
            },
        ),
    ),
);

Module.php

代码语言:javascript
复制
public function onBootstrap($event)
{
    $serviceManager = $event->getApplication()->getServiceManager();
    $serviceManager->get('SessionManager')->start();
}
票数 0
EN

Stack Overflow用户

发布于 2014-04-04 22:02:56

这就是我是如何做到的。我不是世界上最伟大的程序员,但这似乎是可行的。这都是在Module.php中实现的

代码语言:javascript
复制
public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $sharedManager       = $eventManager->getSharedManager();
    $sm = $e->getApplication()->getServiceManager();        
    //This checks to see if the user is logged in. 
    $eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'checkLogin'), 100);


}

//This function is attached to a listener to see if the user is not currently logged in
//If they are not logged in they will be redirected to the login page. This check will happen through the 
//application so there is no need to keep checking in other modules
public function checkLogin (MvcEvent $e)
{



    $session = new Container('defaults');        
    $this->route = $e->getRouteMatch();
    $this->matchedRouteName = explode('/', $this->route->getMatchedRouteName());
    $this->route_root = $this->matchedRouteName[0];
    $sm = $e->getApplication()->getServiceManager();

    $zfcServiceEvents = $sm->get('ZfcUser\Authentication\Adapter\AdapterChain')->getEventManager();

    $zfcServiceEvents->attach(
            'authenticate',
            function ($e) use ($session) {
            $session->offsetSet('sessionstart', $_SERVER['REQUEST_TIME']);
            }
    );  

    $auth = $sm->get('zfcuser_auth_service');

    if (!$auth->hasIdentity() && $this->route_root != 'zfcuser')
     { 
        $response = new \Zend\Http\PhpEnvironment\Response();
        $response->getHeaders()->addHeaderLine('Location', '/user/login');
        $response->setStatusCode(302);
        $response->sendHeaders();
        $e->stopPropagation(true);
        return $response;
    }

   else if ($auth->hasIdentity() && $session->offsetGet('sessionstart') < ($_SERVER['REQUEST_TIME'] - 10800) && $this->route_root != 'zfcuser')
   {
        $response = new \Zend\Http\PhpEnvironment\Response();
        $response->getHeaders()->addHeaderLine('Location', '/user/logout');
        $response->setStatusCode(302);
                $response->sendHeaders();
                $e->stopPropagation(true);
                return $response;
   }

   else if ($auth->hasIdentity())
   {
    $session->offsetSet('sessionstart', $_SERVER['REQUEST_TIME']);
   }    
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21967863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档