2013-05-17 84 views
4

我想爲我的ZF2 v2.2網站實現rememberme功能。 所以這是我迄今所做的: 我創建了一個服務會話管理器寫會話DB:ZF2用戶記住我不工作

'session' => array(
    'remember_me_seconds' => 2419200, 
    'use_cookies' => true, 
    'cookie_httponly' => true, 
), 


     'session_manager' => function (ServiceManager $sm) { 
      $adapter = $sm->get('db_adapter'); 
      $config = $sm->get('app_config'); 
      $sessionOptions = new Session\SaveHandler\DbTableGatewayOptions(); 
      $sessionTableGateway = new TableGateway('tbl_session', $adapter); 
      $saveHandler = new Session\SaveHandler\DbTableGateway($sessionTableGateway, $sessionOptions); 
      $sessionConfig = new Session\Config\SessionConfig(); 
      $sessionConfig->setCookieDomain(ACTIVE_SITE); 
      $sessionConfig->setCookieSecure(true); 
      $sessionConfig->setOptions($config['session']); 
      $sessionManager = new Session\SessionManager($sessionConfig, NULL, $saveHandler); 
      $sessionManager->start(); 
      return $sessionManager; 
     }, 

而且使用該會話管理器,我會和AuthenticationService

Session\Container::setDefaultManager($sm->get('session_manager')); 

     'user_auth_service' => function (ServiceManager $sm) { 
      $authService = new \Zend\Authentication\AuthenticationService(); 
      $session = new \Zend\Authentication\Storage\Session(null, null, $sm->get('session_manager')); 
      $authService->setStorage($session); 
      return $authService; 
     }, 

在我的登錄表單我使用記得我:

public function login(\User\Model\User $user) 
    { 
     $authAdapter = $this->getServiceLocator()->get('user_auth_adapter'); 
     $authAdapter->setIdentity($user->username); 
     $authAdapter->setCredential($user->password); 

     /* @var $authService \Zend\Authentication\AuthenticationService */ 
     $authService = $this->getServiceLocator()->get('user_auth_service'); 
     $result = $authService->authenticate($authAdapter); 

     switch ($result->getCode()) { 

      case \Zend\Authentication\Result::FAILURE_IDENTITY_NOT_FOUND: 
      case \Zend\Authentication\Result::FAILURE_CREDENTIAL_INVALID: 
       return $result->getMessages(); 
       break; 

      case \Zend\Authentication\Result::SUCCESS: 
       $user = $authAdapter->getResultRowObject(null, 'password'); 
       $user->rolls = $this->getServiceLocator()->get('user_role_table')->getRoles($user->id); 
       $authService->getStorage()->write($user); 
       getSM()->get('session_manager')->rememberMe(); 
       return true; 
       break; 

      default: 
       return 'Invalid Credential Provided !'; 
       break; 
     } 
    } 

但應用程序仍然不記得我。什麼我做錯了什麼 這裏 ???

+0

也許你可以使用或從https://github.com/goalio/GoalioRememberMe –

回答

9

編輯:OH RIGHT,我現在記得。 remember_me_seconds是針對服務器的,但如果客戶端刪除了cookie,則這並不好。您應該使用cookie_lifetime和記住選項設置爲適當的值。嘗試以下操作。

session' => array(
    'cookie_lifetime' => 2419200, //SEE ME 
    'remember_me_seconds' => 2419200, //SEE ME 
    'use_cookies' => true, 
    'cookie_httponly' => true, 
), 

讓我知道它是否有效。

忽視以下內容。

我不認爲remember_me選項有效。我看了一下ZF2代碼,這裏有一些跡象表明它是無用的。尋找//SEE ME評論。

public function setStorageOption($storageName, $storageValue) 
{ 
    $key = false; 
    switch ($storageName) { 
     // SEE ME 
     case 'remember_me_seconds': 
      // do nothing; not an INI option 
      return; 
     case 'url_rewriter_tags': 
      $key = 'url_rewriter.tags'; 
      break; 
     default: 
      $key = 'session.' . $storageName; 
      break; 
    } 

    $result = ini_set($key, $storageValue); 
    if (FALSE === $result) { 
     throw new \InvalidArgumentException("'" . $key . 
       "' is not a valid sessions-related ini setting."); 
    } 
    return $this; 
} 


/** 
* Retrieve a storage option from a backend configuration store 
* 
* Used to retrieve default values from a backend configuration store. 
* 
* @param string $storageOption 
* @return mixed 
*/ 
public function getStorageOption($storageOption) 
{ 
    switch ($storageOption) { 
     // SEE ME 
     case 'remember_me_seconds': 
      // No remote storage option; just return the current value 
      return $this->rememberMeSeconds; 
     case 'url_rewriter_tags': 
      return ini_get('url_rewriter.tags'); 
     // The following all need a transformation on the retrieved value; 
     // however they use the same key naming scheme 
     case 'use_cookies': 
     case 'use_only_cookies': 
     case 'use_trans_sid': 
     case 'cookie_httponly': 
      return (bool) ini_get('session.' . $storageOption); 
     default: 
      return ini_get('session.' . $storageOption); 
    } 
}