2014-03-05 153 views

回答

1

首先,您必須註冊用戶擁有的所有單個會話。用戶可以同時使用不同的瀏覽器登錄。對於每個會話,您需要註冊用戶開始會話的時間。

在一些僞代碼:

use Zend\Session\SessionManager; 

public function login($username, $password) 
{ 
    // do some checks to perform the login 
    // now you have a $user available if login is success 

    // Session holds the session id and a timestamp 
    $manager = new SessionManager; 
    $id  = $manager->getId(); 

    $session = $this->registerSession($user, $id); 
} 

然後,你必須檢查在每一個請求,如果當前會話不是僅適用於PHP(默認登錄代碼),而且如果會話仍然有效根據你的TTL(生存時間)。因此,假設您使用getAuthenticatedUser()登錄的用戶,請這樣寫:

use Zend\Session\SessionManager; 

public function getAuthenticatedUser() 
{ 
    $manager = new Sessionmanager; 

    // This is the session id 
    $id = $manager->getId(); 

    // Now get the $session you registered earlier based on $id 

    $now = new DateTime; 
    if ($session->getTTL() > $now) { 
     return false; 
    } 

    // continue and get $user based on $session 
}