如何在 Zend 框架中使用 Zend_auth 时设置超时


How to set the timeout while using Zend_auth in Zend framework

我正在使用Zend_auth进行身份验证。相同的代码如下:

             $authAdapter = $this->getAuthAdapter();
            $authAdapter->setIdentity($username)
                    ->setCredential($password);
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);
            # is the user a valid one?
            if ($result->isValid()) {
                # all info about this user from the login table
                # ommit only the password, we don't need that
                $userInfo = $authAdapter->getResultRowObject(null, 'password');
                # the default storage is a session with namespace Zend_Auth
                $authStorage = $auth->getStorage();
                $authStorage->write($userInfo);
                $emp_id = $userInfo->employee_id;
                $userInfo = Zend_Auth::getInstance()->getStorage()->read();
                $array_db = new Application_Model_SetMstDb();
                $array_name = $array_db->getName($emp_id);
                foreach ($array_name as $name) :
                    $fname = $name['first_name'];
                    $lname = $name['last_name'];
                endforeach;
                $firstname = new stdClass;
                $lastname = new stdClass;
                $userInfo->firstname = $fname;
                $userInfo->lastname = $lname;
                $privilege_id = $userInfo->privilege_id;
                echo 'privilege in Login: ' . $privilege_id;
                $this->_redirect('index/index');
            } else {
                $errorMessage = "Invalid username or password";
                $this->view->error = $errorMessage;
              }

其中 getAuthAdapter() 如下所示:

     protected function getAuthAdapter() {
    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
    $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
    $authAdapter->setTableName('credentials')
            ->setIdentityColumn('employee_id')
            ->setCredentialColumn('password');

    return $authAdapter;
}

我想设置会话超时。我想将超时设置为 5 分钟,当用户 5 分钟未处于活动状态时,会话应该过期,即应调用注销操作,其代码如下:

      public function logoutAction() {
    // action body
    Zend_Auth::getInstance()->clearIdentity();
    $this->_redirect('login/index');
   }

提前谢谢。请帮助我。它很紧急。

当我使用

    $session = new Zend_Session_Namespace( 'Zend_Auth' ); 
    $session->setExpirationSeconds( 60 ); 
控制在 60 秒

后自动重定向到登录页面,但我希望如果应用程序的用户处于非活动状态 60 秒,那么只有它重定向。目前,无论用户是否处于活动状态,都会发生重定向。

我不会为此使用 init()。 init() 应该用于设置对象状态。

我会使用 preDispatch()。但要避免使用它所有控制器或制作基本控制器然后扩展。您可以做一个插件并将其添加到引导程序上。

class YourControllerPlugin extends Zend_Controller_Plugin_Abstract {
   public function preDispatch() {
        //check if expired
       if(hasExpired()) {
          //logout and redirect
       }
   }
}

将其添加到引导程序:

public function __initYourPlugin () {
    $this->bootstrap('frontController');
    $plugin = new YourControllerPlugin();
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin($plugin);
    return $plugin;
}

我现在正在查看我的代码。此代码段来自前端控制器插件。每次经过身份验证的用户请求页面时,我都会重置他们的会话过期时间,以便他们距离上次"活动"有 60 分钟的时间。

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
    //check whether the client is authenticated
    if (Zend_Auth::getInstance()->hasIdentity()) {
        $session = $this->_getAuthSession();
        //update session expiry date to 60mins from NOW
        $session->setExpirationSeconds(60*60);
        return;
    }

旁白:我正在查看此代码,以向用户显示"您的会话已过期"消息,而不是当前的"您未通过身份验证"消息。