当前位置: 首页 > 知识库问答 >
问题:

Codeigniter:Ion验证重置密码

匡玉堂
2023-03-14

我正在使用带有codeigniter的Ion auth库,并创建了登录、注册、记住我和忘记密码功能(到目前为止)。

对于忘记密码功能,用户输入他们的电子邮件地址,然后向他们发送一封电子邮件,其中包含重置密码的链接。

将打开一个页面,输入新密码和确认密码,当我单击“提交”时,php日志中出现以下错误:

PHP Fatal error:  Call to undefined method Auth::_valid_csrf_nonce() in /Applications/MAMP/htdocs/Auth/application/controllers/auth.php on line 273

我下载这个库时没有做任何更改,所以想知道我哪里出错了?

谢谢

下面是我的代码来支持这一点:

认证控制器:

function _get_csrf_nonce()
    {
        $this->load->helper('string');
        $key = random_string('alnum', 8);
        $value = random_string('alnum', 20);
        $this->session->set_flashdata('csrfkey', $key);
        $this->session->set_flashdata('csrfvalue', $value);

        return array($key => $value);
    } 

    //reset password - final step for forgotten password
    public function reset_password($code = NULL)
    {
        if (!$code)
        {
            show_404();
        }

        $user = $this->ion_auth->forgotten_password_check($code);

        if ($user)
        {
            //if the code is valid then display the password reset form

            $this->form_validation->set_rules('new', 'New Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[new_confirm]');
            $this->form_validation->set_rules('new_confirm', 'Confirm New Password', 'required');

            if ($this->form_validation->run() == false)
            {
                //display the form

                //set the flash data error message if there is one
                $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

                $this->data['min_password_length'] = $this->config->item('min_password_length', 'ion_auth');
                $this->data['new_password'] = array(
                    'name' => 'new',
                    'id'   => 'new',
                'type' => 'password',
                    'pattern' => '^.{'.$this->data['min_password_length'].'}.*$'
                );
                $this->data['new_password_confirm'] = array(
                    'name' => 'new_confirm',
                    'id'   => 'new_confirm',
                    'type' => 'password',
                    'pattern' => '^.{'.$this->data['min_password_length'].'}.*$'
                );
                $this->data['user_id'] = array(
                    'name'  => 'user_id',
                    'id'    => 'user_id',
                    'type'  => 'hidden',
                    'value' => $user->id
                );
                $this->data['csrf'] = $this->_get_csrf_nonce();
                $this->data['code'] = $code;

                //render
                $this->_render_page('reset_password', $this->data);
            }
            else
            {
                // do we have a valid request?
                if ($this->_valid_csrf_nonce() === FALSE || $user->id != $this->input->post('user_id'))
                {

                    //something fishy might be up
                    $this->ion_auth->clear_forgotten_password_code($code);

                    show_error('This form post did not pass our security checks.');

                }else{
                    // finally change the password
                    $identity = $user->{$this->config->item('identity', 'ion_auth')};

                    $change = $this->ion_auth->reset_password($identity, $this->input->post('new'));

                    if ($change)
                    {
                        //if the password was successfully changed
                        $this->session->set_flashdata('message', $this->ion_auth->messages());
                        $this->logout();
                    }else{
                        $this->session->set_flashdata('message', $this->ion_auth->errors());
                        redirect('reset_password/' . $code, 'refresh');
                    }
                }
            }
        }
        else
        {
            //if the code is invalid then send them back to the forgot password page
            $this->session->set_flashdata('message', $this->ion_auth->errors());
            redirect("forgot_password", 'refresh');
        }
    }

忘记密码模型功能:

/**
     * Forgotten Password Complete
     *
     * @return string
     * @author Mathew
     **/
    public function forgotten_password_complete($code, $salt=FALSE)
    {
        $this->trigger_events('pre_forgotten_password_complete');

        if (empty($code))
        {
            $this->trigger_events(array('post_forgotten_password_complete', 'post_forgotten_password_complete_unsuccessful'));
            return FALSE;
        }

        $profile = $this->where('forgotten_password_code', $code)->users()->row(); //pass the code to profile

        if ($profile) {

            if ($this->config->item('forgot_password_expiration', 'ion_auth') > 0) {
                //Make sure it isn't expired
                $expiration = $this->config->item('forgot_password_expiration', 'ion_auth');
                if (time() - $profile->forgotten_password_time > $expiration) {
                    //it has expired
                    $this->set_error('forgot_password_expired');
                    $this->trigger_events(array('post_forgotten_password_complete', 'post_forgotten_password_complete_unsuccessful'));
                    return FALSE;
                }
            }

            $password = $this->salt();

            $data = array(
                'password'                => $this->hash_password($password, $salt),
                'forgotten_password_code' => NULL,
                'active'                  => 1,
             );

            $this->db->update($this->tables['users'], $data, array('forgotten_password_code' => $code));

            $this->trigger_events(array('post_forgotten_password_complete', 'post_forgotten_password_complete_successful'));
            return $password;
        }

        $this->trigger_events(array('post_forgotten_password_complete', 'post_forgotten_password_complete_unsuccessful'));
        return FALSE;
    }

重置密码视图:

<div id="infoMessage"><?php echo $message;?></div>

<?php echo form_open('auth/reset_password/' . $code);?>

    <p>
        New Password (at least <?php echo $min_password_length;?> characters long): <br />
        <?php echo form_input($new_password);?>
    </p>

    <p>
        Confirm New Password: <br />
        <?php echo form_input($new_password_confirm);?>
    </p>

    <?php echo form_input($user_id);?>
    <?php echo form_hidden($csrf); ?>

    <p><?php echo form_submit('submit', 'Change');?></p>

<?php echo form_close();?>

共有1个答案

田兴旺
2023-03-14

您的身份验证文件中是否有此方法?

function _valid_csrf_nonce()
{
    if ($this->input->post($this->session->flashdata('csrfkey')) !== FALSE && $this->input->post($this->session->flashdata('csrfkey')) == $this->session->flashdata('csrfvalue')) {
        return TRUE;
    } else {
        return FALSE;
    }
}

如果没有,则添加它。

 类似资料:
  • 所以在任何一个人关闭这个之前,我在这里引用一个答案并提出一个问题。 null 还有什么想法吗?我如何使Laravel7工作,以匹配以上的约束?

  • 我用哈希法通过了一个密码 这将密码作为哈希值存储到数据库中。但是当我试图通过 无论密码是否正确,它都会告诉我密码是正确的。有没有办法解决这个问题,所以我可以散列密码,但登录时输入(非散列)密码。

  • 当我尝试执行代码时,我收到一个无效的密码错误。我想提及的是,我也在使用相同的密码功能进行注册。但是,当我在登录代码中实现它时,我并没有获得成功。请告诉我我做错了什么,并帮助我修复错误。 另一件事是,我输入的密码与注册演示用户时使用的密码相同/正确。这是登录的代码。

  • 有没有办法让用户在重置密码时重置其firebase密码,而无需实际验证帐户? 最初创建帐户时,用户信息中的“已验证”设置为false,一旦创建帐户,我们将收到一封带有验证帐户url的电子邮件。但是,当用户“忘记密码”时,密码重置邮件将发送给用户,当用户实际重置密码时,firebase userinfo“验证”设置为true。

  • 问题内容: 因此,我必须创建验证密码是否有效的代码: 至少8个字符长 包含至少1个数字 包含至少1个大写字母 这是代码: 我不确定出什么问题,但是当我输入带有数字的密码时-它会一直告诉我我需要一个带有数字的密码。有什么办法吗? 问题答案: 您可以将模块用于正则表达式。 有了它,您的代码将如下所示:

  • 主要内容:使用方式,实例演示jQuery Password Validation(密码验证)插件扩展了 jQuery Validate 插件,提供了两种组件: 一种评价密码的相关因素的功能:比如大小写字母的混合情况、字符(数字、特殊字符)的混合情况、长度、与用户名的相似度(可选的)。 一种使用评价功能显示密码强度的验证插件自定义方法。显示的文本可以被本地化。 您可以简单地自定义强度显示的外观、本地化消息显示,并集成到已有的表