【问题标题】:How to log expiration time when a user session expired in Zend Auth如何在 Zend Auth 中记录用户会话过期时的过期时间
【发布时间】:2013-03-16 22:42:22
【问题描述】:

我想将用户登录和注销/会话到期信息添加到数据库中,正常登录和注销很容易,但我不知道如何进行自动会话到期。

我的身份验证如下所示。

我的登录控制器操作

if ($request->isPost()) {
            $data = $request->getParams();
            $userModel = new Application_Model_User_DbTable();
            if ($user = $userModel->login($data['email'], $data['password'])) {
                /* check if user is activated or not */
                if ($user['status'] == 0) {
                    $this->view->loginerror = "<b>Account not active :</b> Please wait for admin to activate your account";
                }elseif($user['status'] == -1){ 
                    $this->view->loginerror = "<b>Account Suspended :</b> Your account is suspeneded you must contact admin to continue";
                }else {
                    /* Store authentication data in session */
                    $auth = Zend_Auth::getInstance();
                    $identity = Zend_Auth::getInstance()->getStorage();
                    $identity->write($user);
                    $this->_redirect('/fax');
                }
            } else {
                $this->view->loginerror = "<b>Invalid login :</b> Email or passsword is invalid !";
            }
        }

在我的用户控件中验证方法

function authenticate($email, $password) {
        $where = array();
        $where[] = $this->getAdapter()->quoteinto('email = ?', $email);
        $user = $this->fetchRow($where);
        if (isset($user['email'])) {
            $salt = $user['password_salt'];
            if (sha1($password . $salt) == $user['password']) {
                /** here i will add login session info**/
                return $user;
            }
            return false;
        }
        return false;
    }

【问题讨论】:

  • 如果用户没有真正“注销”,就无法检查他们的过期时间(您需要另一个 HTTP 请求来检查会话的最后一个时间戳)。但是,如果您的会话持续 20 分钟,那么可以公平地说,在该时间段内未刷新的任何会话都不再处于活动状态,您可以有一个计划任务来定期更新这些数据库记录。

标签: php zend-framework zend-auth zend-session


【解决方案1】:

恐怕没有核心 PHP 或 Zend 函数来执行此操作,会话超时不会在后台运行。除非超时会话发出另一个请求,否则甚至不可能知道会话是否超时。

其中一种方法是向操作发出 ajax 请求以检查超时并在该操作中更新您的数据库。

【讨论】:

    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2018-04-19
    • 2011-11-29
    相关资源
    最近更新 更多