2017-07-23 221 views
0

有没有解决方案如何为登录用户更改密码?我想为登录用户创建一个更改密码,我只修改了用户密码,其中用户ID号为1.它不会为登录用户更改。所以有什么想法?更改登录用户的密码(codeigniter)

这是我的控制器:

public function update(){ 
    $this->form_validation->set_rules('password', 'Current Password', 'required|alpha_numeric|min_length[6]|max_length[20]'); 
    $this->form_validation->set_rules('newpass', 'New Password', 'required|alpha_numeric|min_length[6]|max_length[20]'); 
    $this->form_validation->set_rules('confpassword', 'Confirm Password', 'required|alpha_numeric|min_length[6]|max_length[20]'); 

    if($this->form_validation->run()){ 
     $cur_password = $this->input->post('password'); 
     $new_password = $this->input->post('newpass'); 
     $conf_password = $this->input->post('confpassword'); 
     $this->load->model('queries'); 
     $userid = '1'; 
     $passwd = $this->queries->getCurrPassword($userid); 
     if($passwd->password == $cur_password){ 
      if($new_password == $conf_password){ 
       if($this->queries->updatePassword($new_password, $userid)){ 
        echo 'Password updated successfully'; 
       } 
       else{ 
        echo 'Failed to update password'; 
       } 
      } 
      else{ 
       echo 'New password & Confirm password is not matching'; 
      } 
     } 
     else{ 
      echo'Sorry! Current password is not matching'; 

    } 
} 
else{ 
    echo validation_errors(); 
} 

这是我的模型:

public function getCurrPassword($userid){ 
    $query = $this->db->where(['id'=>$userid]) 
        ->get('users'); 
    if($query->num_rows() > 0){ 
     return $query->row(); 
    } } 

    public function updatePassword($new_password, $userid){ 
    $data = array(
     'password'=> $new_password 
    ); 
     return $this->db->where('id', $userid) 
         ->update('users', $data); } 

回答

0

可以初始化会话当用户登录到系统之初,并存储的信息该会话中的用户如下所示:

function login() { 
    $query = $this->db->select(*) 
         ->from('table_name') 
         ->where('your parameters....'); 
    return $query->row(); 
} 

function index() { 
    $userid = $this->login()->id; //id of the user which is currently logged IN 
    $this->session->set_userdata('current_userId', $userid); 
} 

您现在已经存储了curren的id tly在会话中登录IN用户。您可以通过$this->session->userdata('current_userId');访问该ID。用会话数据替换您的$userid = '1'。此外,您将不得不加载会话库才能这样做。