2010-10-15 52 views
7

我使用Zend_Auth作爲我的項目之一,但到目前爲止還沒有想出如何設置會話的生存期,或如何擴展它(讓我們說它應該運行5分鐘,應該重置,當用戶進行操作),這裏是我的初始化代碼:使用Zend_Auth設置和擴展會話生命週期

 $authAdapter = new Zend_Auth_Adapter_DbTable($this->_model->pdo); 
     $authAdapter->setTableName('normal_folks') 
      ->setIdentityColumn('username') 
      ->setCredentialColumn('password'); 

     $post = $this->_request->getPost(); 

     $authAdapter->setIdentity($post['username']) 
      ->setCredential($post['password']); 
     $auth = Zend_Auth::getInstance(); 
     $result = $auth->authenticate($authAdapter); 

     if($result->isValid()) 
     { 
      $userInfo = $authAdapter->getResultRowObject(null, 'password'); 
      $authStorage = $auth->getStorage(); 
      $authStorage->write($userInfo); 

      if(strlen($post['refferer']) > 1){ 
       header("Location: ".$post['refferer']); 
      }elseif(strlen($this->_request->getParam('ref_action')) > 1){ 
       Zend_Controller_Action::_forward($this->_request->getParam('ref_action'),"admin",null,null); 
      }else{ 
       Zend_Controller_Action::_forward("index","admin",null,null); 
      } 
     } 

螞蟻這個我怎麼檢查,如果用戶登錄:

   if(Zend_Auth::getInstance()->hasIdentity()){ 
        echo "Woho!"; 
       }else{ 
        die("invalid-identity"); 
       } 

它大概正確的,在前面的我,但我無法弄清楚,幫助?請?漂亮嗎? :D

回答

13

身份驗證狀態存儲在已註冊的身份驗證存儲中。默認情況下,這是Zend_Session。您可以set an expiration time to the Zend_Auth namespace,例如還可以globally configure Zend_Session通過

Zend_Session::setOptions(array(
    'cookie_lifetime' => 300, 
    'gc_maxlifetime' => 300)); 
+0

同時,任何我怎麼能「刷新」該在行動的情況下的一生? – Hannes 2010-10-15 08:23:45

+0

@Hannes我認爲到期時間會隨着每個請求自動刷新,所以只需更新頁面就可以再給你300秒。 – Gordon 2010-10-15 08:33:56

+0

btw。 ); $ namespace = new Zend_Session_Namespace('Zend_Auth');'是的,你是對的,它每次被調用時都會重置,無論出於什麼原因你的第二個解決方案不工作(把它放在init()中) - 但第一個工作只是丹迪:D非常感謝! – Hannes 2010-10-15 08:42:17

1

如果您正在使用不同的命名空間Zend_Auth的會議

$namespace = new Zend_Session_Namespace('Zend_Auth'); 
$namespace->setExpirationSeconds(300); 

你可以做這樣的:

$auth = Zend_Auth::getInstance(); 
$auth->setStorage (new Zend_Auth_Storage_Session ('user')); 

$namespace = new Zend_Session_Namespace('user'); 
$namespace->setExpirationSeconds(7200); // 2 hours 
+0

感謝您的維修人員,雖然我無法驗證它,因爲相關應用程序不再維護 – Hannes 2015-06-29 14:56:12