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

问题描述 投票:-1回答:2

我使用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');
   }

在此先感谢.Plz帮帮我。很紧急。

我用的时候

    $session = new Zend_Session_Namespace( 'Zend_Auth' ); 
    $session->setExpirationSeconds( 60 ); 

控制在60秒后自动重定向到登录页面,但我希望如果应用程序的用户处于非活动状态60秒,则仅重定向。目前是否用户是否处于活动状态。

php zend-framework zend-auth
2个回答
0
投票

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

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

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

在Bootstrap上添加它:

public function __initYourPlugin () {
    $this->bootstrap('frontController');

    $plugin = new YourControllerPlugin();

    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin($plugin);

    return $plugin;
}

0
投票

我现在正在看我的代码。此代码段来自前端控制器插件。每次经过身份验证的用户请求页面时,我都会重置其会话到期时间,因此他们距离上次“活动”时间为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;
    }

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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.