Zend framework2 Zend/与ZfcUser的会话
我在应用程序中使用ZfcUser,我需要控制超时参数。因为它不是配置的一部分,所以我想在引导时将我自己的Zend/Session对象(使用Zend framework2 Zend/与ZfcUser的会话,zend-framework2,zend-session,zfcuser,Zend Framework2,Zend Session,Zfcuser,我在应用程序中使用ZfcUser,我需要控制超时参数。因为它不是配置的一部分,所以我想在引导时将我自己的Zend/Session对象(使用记住我的秒数参数)设置为ZfcUser,但我不知道如何设置 顺便问一下,有人已经这样做了吗?我不使用zfcuser,但试试这个 autoload/global.php <?php use Zend\Session\Config\SessionConfig; use Zend\Session\SessionManager; use Zend\Sessio
记住我的秒数
参数)设置为ZfcUser,但我不知道如何设置
顺便问一下,有人已经这样做了吗?我不使用zfcuser,但试试这个 autoload/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();
}
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']);
}
}
bootstrap上的公共函数(MvcEvent$e)
{
$eventManager=$e->getApplication()->getEventManager();
$sharedManager=$eventManager->getSharedManager();
$sm=$e->getApplication()->getServiceManager();
//这将检查用户是否已登录。
$eventManager->attach(MvcEvent::EVENT_DISPATCH,array($this,'checkLogin'),100);
}
//此函数附加到侦听器,以查看用户当前是否未登录
//如果他们没有登录,他们将被重定向到登录页面。这项检查将通过
//应用程序,因此无需继续检入其他模块
公共功能检查登录(MvcEvent$e)
{
$session=新容器(“默认值”);
$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(
“验证”,
函数($e)使用($session){
$session->offsetSet('sessionstart',$_服务器['REQUEST_TIME']);
}
);
$auth=$sm->get('zfcuser\u auth\u service');
如果(!$auth->hasintity()&&$this->route_root!=“zfcuser”)
{
$response=new\Zend\Http\PhpEnvironment\response();
$response->getHeaders()->addHeaderLine('Location','/user/login');
$response->setStatusCode(302);
$response->sendHeaders();
$e->stopPropagation(真);
返回$response;
}
否则,如果($auth->hasintity()&&&$session->offsetGet($sessionstart')<($\u SERVER['REQUEST\u TIME']-10800)&&&&$this->route\u root!=“zfcuser”)
{
$response=new\Zend\Http\PhpEnvironment\response();
$response->getHeaders()->addHeaderLine('Location','/user/logout');
$response->setStatusCode(302);
$response->sendHeaders();
$e->stopPropagation(真);
返回$response;
}
else if($auth->haseIdentity())
{
$session->offsetSet('sessionstart',$_服务器['REQUEST_TIME']);
}
}
设置会话参数的最简单方法如下
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,
],
);
public function onBootstrap(MvcEvent $e)
{
$manager = $e->getApplication()->getServiceManager()->get('Zend\Session\ManagerInterface');
}
并在Module.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,
],
);
public function onBootstrap(MvcEvent $e)
{
$manager = $e->getApplication()->getServiceManager()->get('Zend\Session\ManagerInterface');
}
这将影响会话设置,并且phpinfo()
会显示它
我在中找到的,恐怕比这更复杂。会话必须以某种方式链接到ZfcUser。。。