未定义的索引:令牌错误

时间:2017-09-10 12:04:46

标签: cakephp cakephp-1.3 cakephp-1.2

我在这里使用github存储库代码:https://github.com/hunzinker/CakePHP-Auth-Forgot-Password

我在UsersController.php中使用了以下功能。我在前面有注释的行上得到错误Undefined index:token。我应该改变什么?

/**
 * Allow user to reset password if $token is valid.
 * @return
 */
function reset_password_token($reset_password_token = null) {
    if (empty($this->data)) {
        $this->data = $this->User->findByResetPasswordToken($reset_password_token);

        if (!empty($this->data['User']['reset_password_token']) &&
            !empty($this->data['User']['token_created_at']) &&
            $this->__validToken($this->data['User']['token_created_at'])
        ) {
            $this->data['User']['id'] = null;
            $_SESSION['token'] = $reset_password_token;
        } else {
            $this->Session->setflash(
                'The password reset request has either expired or is invalid.'
            );
            $this->redirect('/users/login');
        }
    } else {
        //ERROR ON THE NEXT LINE HERE UNDEFINED INDEX: TOKEN
        if ($this->data['User']['reset_password_token'] != $_SESSION['token']) {
            $this->Session->setflash(
                'The password reset request has either expired or is invalid.'
            );
            $this->redirect('/users/login');
        }

        $user = $this->User->findByResetPasswordToken(
            $this->data['User']['reset_password_token']
        );
        $this->User->id = $user['User']['id'];

        if ($this->User->save($this->data, array('validate' => 'only'))) {
            $this->data['User']['reset_password_token'] =
                $this->data['User']['token_created_at'] = null;

            if ($this->User->save($this->data) &&
                $this->__sendPasswordChangedEmail($user['User']['id'])
            ) {
                unset($_SESSION['token']);
                $this->Session->setflash(
                    'Your password was changed successfully. Please login to continue.'
                );
                $this->redirect('/users/login');
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要确保 $ _ SESSION 包含此索引,因此您应该像这样更新它以确保它存在:

由此:

if (!isset($_SESSION['token']) || $this->data['User']['reset_password_token'] != $_SESSION['token']) {
    $this->Session->setflash(
        'The password reset request has either expired or is invalid.'
    );
    $this->redirect('/users/login');
}