2017-07-23 252 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'。此外,您將不得不加載會話庫才能這樣做。