2013-10-29 75 views
0

我正在嘗試更改默認會話超時值。在我的控制器中我已經做到了這一點:Yii Session沒有越過

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 
     ), 

但同樣的結果。會話劑量超時!我錯過了什麼嗎?

回答

1

嘗試CONFIGS關閉自動啓動會話:

'session' => array(
     'sessionName' => SITE_SESSION_COOKIE_NAME, 
     'class' => 'CHttpSession', 
     'autoStart' => false 
    ), 

在這種情況下,你需要手動啓動會話:Yii::app()->session->open(),但在此之前它改變生活時間試試這樣做:

Yii::app()->session->open($session_lifetime); 
$cook_p = Yii::app()->session->getCookieParams(); 
$cook_p['lifetime'] = $session_lifetime; 
Yii::app()->session->setCookieParams($cook_p); 

,或者您可能繼承CHttpSession新的參數lifetime,並做到在方法init()

class MyHttpSession extends CHttpSession{ 
    public $lifetime = false; 
    public function init() 
    { 
     if($this->lifetime !== false){ 
      $cook_p = $this->getCookieParams(); 
      $cook_p['lifetime'] = $this->lifetime; 
      $this->setCookieParams($cook_p); 
      $this->setTimeout($this->lifetime); 
     } 
     parent::init(); 
    } 

}

和CONFIGS:

'session' => array(
     'sessionName' => SITE_SESSION_COOKIE_NAME, 
     'class' => 'MyHttpSession', 
     'lifetime' => 60 // 1 minute 
    ), 
+0

但是當我使用'autoStart'=> false時,即使在第一個請求中,會話也不會啓動,並且我得到未定義的變量:_SESSION –

+0

是的,您是對的。會話的生存期可以在開始會話之前更改。這就是爲什麼我的提議是手動啓動會話,或者您可以繼承HttpSession(請參閱我如何更新我的文章) – CreatoR

+0

以及該文件放置在何處? –

1

session陣列中的class顯然應該是CDbHttpSession這個工作。 了類似的問題。請參見here

+0

如果我使用CDbHttpSession我得到錯誤「CDbConnection無法打開數據庫連接:找不到驅動程序「 –

+0

是的,CDbHttpSession需要數據庫連接,如名稱所示。相關細節列在[here](http://www.yiiframework.com/doc/api/1.1/CDbHttpSession)。 –

0

爲會話超時基於用戶在活躍的是30分鐘,在CONFIGS:

'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 
     ), 

擴展會話類,類似的想法,可用於CDbHttpSession

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

}