2014-02-23 56 views
0

我在我的应用程序中使用ZfcUser,我需要控制超时参数。由于它不是配置的一部分,我想在自举中设置自己的Zend/Session对象(使用remember_me_seconds参数)到ZfcUser,但我不知道如何。Zend /会话与ZfcUser

偶然,有没有人做过这件事?

回答

0

我不使用zfcuser但尝试这个

自动加载/ global.php

<?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

public function onBootstrap($event) 
{ 
    $serviceManager = $event->getApplication()->getServiceManager(); 
    $serviceManager->get('SessionManager')->start(); 
} 
+0

恐怕它比这更复杂。会话必须以某种方式链接到ZfcUser ... –

+0

你试过了吗? – user291196

0

这里是我做到了。我不是世界上最伟大的编码器,但这似乎工作。这是所有在Module.php

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']); 
    }  
} 
1

设置会话PARAMS最简单的方法是如下 config\autoload\global.php

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.php

public function onBootstrap(MvcEvent $e) 
{ 
    $manager = $e->getApplication()->getServiceManager()->get('Zend\Session\ManagerInterface'); 
} 

这将增加行onBootstrap方法影响会话设置并且phpinfo()显示它。

我发现它在zfcuser github