我需要创建一个函数来检查旧密码并更新当前密码

时间:2017-04-21 16:18:51

标签: php html codeigniter

我已经在我的网站上创建了一个登录和注册系统。我的数据库中有一个表格,其中存储了所有用户信息,称为“用户”。我搜索了很多但却无法获得太多。

我的班级

     public function change_password()
{

    $this->load->view('templates/header');
    $this->load->view('registration/changepassword');
    $this->load->view('templates/footer');
}

public function change()
{
    $this->form_validation->set_rules('newpassword', 'New Password', 'required|matches[rpassword]');
    $this->form_validation->set_rules('rpassword', 'Retype Password', 'required');

    if ($this->form_validation->run() == FALSE) {
       redirect('registration/change_password');
    }else{
        $query = $this->Login_Database->checkOldPass(sha1($this->input->post('oldpassword')));
        if($query){
            $query = $this->Login_Database->saveNewPass(sha1($this->input->post('newpassword')));
            if($query){
                redirect('registration/change_password');
            }else{
                redirect('registration/change_password');
            }
        }
        redirect('');


    }


}

我的模特

     // Insert registration data in database
public function checkOldPass($old_password)
{

    $this->db->select('*');

    $this->db->from('users');
    $this->db->select('id');
    $this->db->where('id', 'id');
    $this->db->where('password', $this->input->post('oldpassword'));
    $query = $this->db->get();
    if ($query->num_rows > 0) {
        return true;
    } else {
        $this->form_validation->set_message('checkOldPassword', 'wrong old password.');
        return false;
    }
}

public function saveNewPass($new_pass)
{
    $data = array(
        'password' => $new_pass
    );
    $this->db->where('id', 'id');
    $this->db->update('users', $data);
    return true;
}

}

我的表单

   <h1><p class="text-center" style="font-family: 'Passion One', 
   cursive;">Change Password</p></h1>
   <div>
   <form class="form-horizontal" method="post"
      action="<?php echo base_url() ?>Registration/change">
   <fieldset>
        <br>
        <div class="form-group">
            <label class="col-md-4 control-label" for="phone">Old   
    Password</label>
            <div class="col-md-4">
                <input id="oldpassword" name="oldpassword" type="password" 
     placeholder="Old Password"
                       class="form-control input-md"
                       required="">
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label" for="phone">New 
      Password</label>
            <div class="col-md-4">
                <input id="newpassword" name="newpassword" type="password" 
      placeholder="New Password"
                       class="form-control input-md"
                       required="">
            </div>
        </div>
       <div class="form-group">
            <label class="col-md-4 control-label" for="phone">Conform 
      Password</label>
            <div class="col-md-4">
                <input id="rpassword" name="rpassword" type="password" 
      placeholder="Retype Password"
                       class="form-control input-md"
                       required="">
            </div>
        </div>
        <br>
        <!-- Button (Double) -->
        <div class="form-group">
            <label class="col-md-4 control-label" for="save"></label>
            <div class="col-md-8">
                <button type="submit" class="btn btn-success">Save</button>
                <a id="cancel" name="cancel" class="btn btn-danger" href="<?
     php echo base_url(); ?>">
                    Cancel</a><br><br>
            </div>
    <br><br><br>
    </fieldset>
    </form>

    </div>

我的班级在一个名为Registration的课程内。任何帮助都会非常感激。我的模型的名称也是&#39; Login_Database.php&#39;我的表单名称是&#39; changepassword.php&#39;

1 个答案:

答案 0 :(得分:-1)

请在控制器和您的型号中使用以下代码。如果您有任何问题,请回复我。

//控制器功能

public function change(){
    if ($this->form_validation->run('update_password') == True)
    {
        $old_password = sha1($_POST['oldpassword']);
        unset($_POST['oldpassword']);
        $post = $this->input->post(); 
        $status = $this->model_name->check_old_password($old_password, $user_id); //check old password
        if($status==true){

            if($_POST['newpassword']==$_POST['rpassword']){

                if($this->model_name->update_password($post, $user_id)){
                    $this->session->set_flashdata('message_success', "Password update successully.");
                    redirect('registration/change_password');
                }else{
                    $this->session->set_flashdata('article_failed', "Password not update successully.");
                    redirect('registration/change_password');
                }

            }else{
                $this->session->set_flashdata('article_failed', "Sorry your new password and confirm password does not matched.");
                redirect('registration/change_password');
            }

        }
    }else{
        $this->session->set_flashdata('article_failed', "Sorry your old password is wrong.");
        redirect('registration/change_password');
    }
}

// model function
public function check_old_password($old_password, $user_id){
    $query = $this->db->select('id')
                      ->from('users')
                      ->where(['id'=>$user_id, 'password'=>$old_password])
                      ->get()
                      ->row();

        if($query>0){
            return TURE;
        }else{
            return FALSE;
        }
}

public function update_password($post, $user_id){
    $password = sh1($post['newpassword']);
    return $this->db->where('id', $user_id)
                    ->update('users', ['password'=>$password]);
}