gpt4 book ai didi

Codeigniter - 忘记密码

转载 作者:行者123 更新时间:2023-12-03 15:02:19 26 4
gpt4 key购买 nike

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

  • 恢复 View 提示收到电子邮件输入
  • 功能 email_exists()将验证电子邮件。如果是这样,send_email()$temp_pass key 和链接。数据库将存储 $temp_pass 以供进一步操作和验证。
  • 用户点击之前发送的链接,通过 $temp_pass函数reset_password。
  • 模型 Controller 将验证 $temp_pass与数据库。如果是这样,请加载 View 以输入新密码 - 这就是我卡住的地方,因为表单指向无法识别的 Controller $temp_pass因此无法重置密码。

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

    代码如下:

    Controller
        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";
    }
    }

    模型用户
        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;
    }

    最佳答案

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

    在这一步之后,你要求用户输入他的新密码,因为你有一个临时标志,你称之为 $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

    我猜你犯了一个错误

    另外,我刚刚注意到你的 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>";

    上面那行不应该是 -
    $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>";

    更新

    您可以通过 $temp_pass进入 session 并从那里检索。这是一种方法。

    关于Codeigniter - 忘记密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180462/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com