Codeigniter - 忘记密码

crg*_*821 5 passwords codeigniter

您好:我需要在登录页面上实现忘记密码.在这里,我解释了到目前为止我所拥有的

  1. 恢复视图提示收到电子邮件输入
  2. 功能email_exists()将验证电子邮件.如果是这样,send_email()使用$temp_pass密钥和链接.数据库将存储$ temp_pass以进行进一步的操作和验证.
  3. 用户点击之前发送的链接,传递$temp_pass给函数reset_password.
  4. 模型控制器将验证$temp_pass数据库.如果是这样,加载视图以输入新密码 - 这就是我被卡住的地方,因为表单指向一个无法识别的控制器$temp_pass因此无法重置密码.

如何检索与正确用户关联的新密码并重置密码?

代码如下:

调节器

    public function recover(){
    //Loads the view for the recover password process.
    $this->load->view('recover');
}
public function recover_password(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|callback_validate_credentials');

            //check if email is in the database
        $this->load->model('model_users');
        if($this->model_users->email_exists()){
            //$them_pass is the varible to be sent to the user's email 
            $temp_pass = md5(uniqid());
            //send email with #temp_pass as a link
            $this->load->library('email', array('mailtype'=>'html'));
            $this->email->from('user@yahoo.com', "Site");
            $this->email->to($this->input->post('email'));
            $this->email->subject("Reset your Password");

            $message = "<p>This email has been sent as a request to reset our password</p>";
            $message .= "<p><a href='".base_url()."main/reset_password/$temp_pass'>Click here </a>if you want to reset your password,
                        if not, then ignore</p>";
            $this->email->message($message);

            if($this->email->send()){
                $this->load->model('model_users');
                if($this->model_users->temp_reset_password($temp_pass)){
                    echo "check your email for instructions, thank you";
                }
            }
            else{
                echo "email was not sent, please contact your administrator";
            }

        }else{
            echo "your email is not in our database";
        }
}
public function reset_password($temp_pass){
    $this->load->model('model_users');
    if($this->model_users->is_temp_pass_valid($temp_pass)){

        $this->load->view('reset_password');

    }else{
        echo "the key is not valid";    
    }

}
public function update_password(){
    $this->load->library('form_validation');
        $this->form_validation->set_rules('password', 'Password', 'required|trim');
        $this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
            if($this->form_validation->run()){
            echo "passwords match"; 
            }else{
            echo "passwords do not match";  
            }
}
Run Code Online (Sandbox Code Playgroud)

Model_users

    public function email_exists(){
    $email = $this->input->post('email');
    $query = $this->db->query("SELECT email, password FROM users WHERE email='$email'");    
    if($row = $query->row()){
        return TRUE;
    }else{
        return FALSE;
    }
 }
public function temp_reset_password($temp_pass){
    $data =array(
                'email' =>$this->input->post('email'),
                'reset_pass'=>$temp_pass);
                $email = $data['email'];

    if($data){
        $this->db->where('email', $email);
        $this->db->update('users', $data);  
        return TRUE;
    }else{
        return FALSE;
    }

}
public function is_temp_pass_valid($temp_pass){
    $this->db->where('reset_pass', $temp_pass);
    $query = $this->db->get('users');
    if($query->num_rows() == 1){
        return TRUE;
    }
    else return FALSE;
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*ket 0

我不太确定你被困在哪里。我可以了解到您正在为用户创建一个临时标志,当用户单击链接时您会验证该标志。因此,这意味着您可以在此时开始会话,并且仅当该特定会话处于活动状态时,用户才能重置密码。

在此步骤之后,您要求用户输入他的新密码,并且由于您有为该$temp_pass用户调用的临时标志(请注意它应该是唯一的),因此您可以获取尝试重置的用户密码。

因此,您需要做的就是运行此类数据库查询 -

$this->db->where('reset_pass', $temp_pass);
$this->db->update('users', $data); // where $data will have the fields with values you are updating
Run Code Online (Sandbox Code Playgroud)

我猜你犯了一个错误

recover_password()另外,我刚刚在你的函数中注意到-

$message .= "<p><a href='".base_url()."main/reset_password/$temp_pass'>Click here </a>if you want to reset your password, if not, then ignore</p>";
Run Code Online (Sandbox Code Playgroud)

上面这行不应该是 -

$message .= "<p><a href='".base_url()."main/reset_password/".$temp_pass."'>Click here </a>if you want to reset your password, if not, then ignore</p>";
Run Code Online (Sandbox Code Playgroud)

更新

您可以$temp_pass进入会话并从那里检索。这是解决这个问题的一种方法。

  • 实际上,PHP 在双引号字符串中进行变量替换,因此第一个版本实际上应该可以工作,认为使用带大括号的变体被认为是很好的做法:“main/reset_password/{$temp_pass}'&gt;Clic...” (2认同)