Php Yii会话没有结束

Php Yii会话没有结束,php,session,yii,Php,Session,Yii,我正在尝试更改默认会话超时值。在我的控制器中,我完成了以下操作: public function beforeAction($action) { $session = new CHttpSession; $timeout = $session->getTimeout(); if ($timeout != 10) { $session->setTimeout(10); } return true; } 但我的会话从未超时,即

我正在尝试更改默认会话超时值。在我的控制器中,我完成了以下操作:

  public function beforeAction($action) {
    $session = new CHttpSession;
    $timeout = $session->getTimeout();
    if ($timeout != 10) {
      $session->setTimeout(10);
    }
    return true;
  }
但我的会话从未超时,即使在10秒钟内处于非活动状态,我也可以访问该页面

我还尝试通过按会话组件进行配置,如下所示:

   'session' => array(
            'sessionName' => SITE_SESSION_COOKIE_NAME,
            'class' => 'CHttpSession',
            'timeout' => 10
        ),

但同样的结果。会话超时!我错过了什么吗?

会话中的
显然应该是
CDbHttpSession
,这样才能工作。
有关类似问题,请参阅。

尝试在配置中关闭自动启动会话:

'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=> true,
            'autoRenewCookie'=> true,
            'authTimeout' => 1800
        ),
        'session' => array(
                    'class' => 'FrontCHttpSession',
                    'savePath' => dirname(__FILE__),
                    'cookieMode' => 'allow',
                    'cookieParams' => array(
                            'path' => '/',
                            'domain' => 'mydomain.com',
                            'httpOnly' => true,
                            'lifetime' => 1800
                    ),
                    'timeout' => 1800
        ),
在这种情况下,您需要手动启动会话:
Yii::app()->session->open()
,但在更改生命周期之前,请尝试执行以下操作:

或者,您可以使用新参数
life
继承HttpSession,并在方法
init()
中执行此操作:

}

在配置中:

'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=> true,
            'autoRenewCookie'=> true,
            'authTimeout' => 1800
        ),
        'session' => array(
                    'class' => 'FrontCHttpSession',
                    'savePath' => dirname(__FILE__),
                    'cookieMode' => 'allow',
                    'cookieParams' => array(
                            'path' => '/',
                            'domain' => 'mydomain.com',
                            'httpOnly' => true,
                            'lifetime' => 1800
                    ),
                    'timeout' => 1800
        ),

对于基于用户处于活动状态30分钟的会话超时,在配置中:

'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=> true,
            'autoRenewCookie'=> true,
            'authTimeout' => 1800
        ),
        'session' => array(
                    'class' => 'FrontCHttpSession',
                    'savePath' => dirname(__FILE__),
                    'cookieMode' => 'allow',
                    'cookieParams' => array(
                            'path' => '/',
                            'domain' => 'mydomain.com',
                            'httpOnly' => true,
                            'lifetime' => 1800
                    ),
                    'timeout' => 1800
        ),
扩展会话类,类似的想法可用于CDBHTTP会话

<?php

class FrontCHttpSession extends CHttpSession
{

      /*default is 0 which means the cookie lifetime will last as long as the browser is open*/
      private $_clientLifetime;
      /*time in seconds how long the session should remain open after user in-activity*/
      private $_sessionTimeout;
      /*cookie params defined in config*/
      private $_cookieParams;

      /**
      * Starts the session if it has not started yet.
      */

      public function open()
      {

            $this->_cookieParams = $this->getCookieParams();
            $this->_clientLifetime = $this->_cookieParams['lifetime'];
            $this->_sessionTimeout = $this->timeout;

            if($this->getUseCustomStorage())
                  @session_set_save_handler(array($this,'openSession'),
                        array($this,'closeSession'),
                        array($this,'readSession'),
                        array($this,'writeSession'),
                        array($this,'destroySession'),
                        array($this,'gcSession'));

            //session is already started, check if session has been not been active longer than timeout     
            if (session_id() != '')
            {
                  if ($this->get('last_active') < time() - $this->_sessionTimeout)
                  {
                      $this->destroy();
                  }
                  else if ($this->_clientLifetime > 0)
                  {
                      $this->updateSessionCookieExpire();
                  }
            }

            @session_set_cookie_params($this->_clientLifetime, array($this->_cookieParams['path'], 
                  $this->_cookieParams['domain'], $this->_cookieParams['secure'], $this->_cookieParams['httpOnly']));

            @session_start();
            $this->add('last_active', time());

            if(YII_DEBUG && session_id()=='')
            {

                  $message=Yii::t('yii','Failed to start session.');

                  if(function_exists('error_get_last'))

                  {

                        $error=error_get_last();

                        if(isset($error['message']))

                              $message=$error['message'];

                  }

                  Yii::log($message, CLogger::LEVEL_WARNING, 'system.web.CHttpSession');

            }

      }

      public function updateSessionCookieExpire() 
      {
            if (isset(Yii::app()->request->cookies[$this->getSessionName()])) 
            {
                  $c = Yii::app()->request->cookies[$this->getSessionName()]; 
                  $c->expire = time() + $this->_clientLifetime;
                  $c->path = $this->_cookieParams['path'];
                  $c->domain = $this->_cookieParams['domain'];
                  $c->httpOnly = $this->_cookieParams['httponly'];
                  $c->secure = $this->_cookieParams['secure'];
                  Yii::app()->request->cookies[$this->getSessionName()] = $c;
            }
      }

}

如果我使用CDbHttpSession,我会收到错误“CDbConnection无法打开数据库连接:找不到驱动程序”是的,CDbHttpSession需要一个数据库连接,顾名思义。列出了相关的详细信息。但是当我使用'autoStart'=>false时,会话即使在第一个请求中也不会启动,并且我得到了未定义的变量:_sessionies,你是对的。可以在启动会话之前更改会话的生存期。这就是为什么我的建议是手动启动会话,或者您可以继承HttpSession(请参阅如何更新我的帖子)以及该文件将放置在何处?您可以将其放置在
/protected/components
或其他目录中,但必须导入它的目录(在
导入
参数中)。这给了我未定义的属性:CWebApplication:$session
'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=> true,
            'autoRenewCookie'=> true,
            'authTimeout' => 1800
        ),
        'session' => array(
                    'class' => 'FrontCHttpSession',
                    'savePath' => dirname(__FILE__),
                    'cookieMode' => 'allow',
                    'cookieParams' => array(
                            'path' => '/',
                            'domain' => 'mydomain.com',
                            'httpOnly' => true,
                            'lifetime' => 1800
                    ),
                    'timeout' => 1800
        ),
<?php

class FrontCHttpSession extends CHttpSession
{

      /*default is 0 which means the cookie lifetime will last as long as the browser is open*/
      private $_clientLifetime;
      /*time in seconds how long the session should remain open after user in-activity*/
      private $_sessionTimeout;
      /*cookie params defined in config*/
      private $_cookieParams;

      /**
      * Starts the session if it has not started yet.
      */

      public function open()
      {

            $this->_cookieParams = $this->getCookieParams();
            $this->_clientLifetime = $this->_cookieParams['lifetime'];
            $this->_sessionTimeout = $this->timeout;

            if($this->getUseCustomStorage())
                  @session_set_save_handler(array($this,'openSession'),
                        array($this,'closeSession'),
                        array($this,'readSession'),
                        array($this,'writeSession'),
                        array($this,'destroySession'),
                        array($this,'gcSession'));

            //session is already started, check if session has been not been active longer than timeout     
            if (session_id() != '')
            {
                  if ($this->get('last_active') < time() - $this->_sessionTimeout)
                  {
                      $this->destroy();
                  }
                  else if ($this->_clientLifetime > 0)
                  {
                      $this->updateSessionCookieExpire();
                  }
            }

            @session_set_cookie_params($this->_clientLifetime, array($this->_cookieParams['path'], 
                  $this->_cookieParams['domain'], $this->_cookieParams['secure'], $this->_cookieParams['httpOnly']));

            @session_start();
            $this->add('last_active', time());

            if(YII_DEBUG && session_id()=='')
            {

                  $message=Yii::t('yii','Failed to start session.');

                  if(function_exists('error_get_last'))

                  {

                        $error=error_get_last();

                        if(isset($error['message']))

                              $message=$error['message'];

                  }

                  Yii::log($message, CLogger::LEVEL_WARNING, 'system.web.CHttpSession');

            }

      }

      public function updateSessionCookieExpire() 
      {
            if (isset(Yii::app()->request->cookies[$this->getSessionName()])) 
            {
                  $c = Yii::app()->request->cookies[$this->getSessionName()]; 
                  $c->expire = time() + $this->_clientLifetime;
                  $c->path = $this->_cookieParams['path'];
                  $c->domain = $this->_cookieParams['domain'];
                  $c->httpOnly = $this->_cookieParams['httponly'];
                  $c->secure = $this->_cookieParams['secure'];
                  Yii::app()->request->cookies[$this->getSessionName()] = $c;
            }
      }

}