【问题标题】:How to set the timeout while using Zend_auth in Zend framework在 Zend 框架中使用 Zend_auth 时如何设置超时
【发布时间】:2012-05-03 22:57:55
【问题描述】:

我使用 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 秒内处于非活动状态,那么只有它会重定向。目前无论用户是否处于活动状态,都会发生重定向。

【问题讨论】:

  • 如果我将使用 $session = new Zend_Session_Namespace( 'Zend_Auth' ); $session->setExpirationSeconds(60);那么如何检查 60 秒是否已经完成以及在哪里检查它,就像它必须在每个控制器的 init() 方法中检查一样。
  • 请给一些代码sn-p

标签: php zend-framework zend-auth


【解决方案1】:

我不会为此使用 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;
}

【讨论】:

  • 这里 $this->bootstrap('frontController');必须按原样使用,否则我必须指定我的前端控制器 loginController??
  • 不是你的 LoginController。这只是为了确保我们初始化 Zend 的前端控制器,该控制器负责请求环境。您使用的框架是什么版本的?
  • 您好,我没听懂。请回答以下问题:1.我应该在哪里定义会话及其到期时间??2.应该在引导文件中定义吗?3.以上两个sn -p 必须在引导文件中指定??
【解决方案2】:

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

另外:我正在查看此代码以寻找一种向用户显示“您的会话已过期”消息而不是当前“您未通过身份验证”消息的方法。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-02-12
  • 2010-12-29
  • 2012-03-05
  • 2014-08-11
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多