2017-09-14 130 views
1

我想問如何更改登錄用戶的密碼, 當我輸入與數據庫中的任何人相匹配的密碼時,我可以更改密碼。 僅舉例,用戶有「admin」密碼,我只需輸入當前密碼, 新密碼並確認密碼。Codeigniter:更改登錄用戶的密碼和使用md5的密碼

當前密碼:admin 新密碼:newadmin 當前密碼:新的管理

同時,我不知道如何更改密碼,如果密碼 使用MD5()。我希望你能幫助我,我是Codeigniter的新手。 我搜索的答案,但我真的不明白它,所以我想評論,但 它需要50聲望,所以我發佈了新的問題。

這裏是我的代碼:

控制器

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

是你的密碼數據存儲爲MD5在你的數據庫? – sintakonte

+0

@sintakonte是的。 – squaredsquared

+0

使用PHP使用'password_hash'和'password_verify'。討論:僅使用散列函數保存密碼驗證者是不夠的,僅僅添加鹽對提高安全性沒有多大作用。相反,用隨機鹽迭代HMAC約100ms持續時間,然後用散列表保存鹽。使用諸如PBKDF2,Rfc2898DeriveBytes,password_hash,Bcrypt,passlib.hash或類似函數的函數。關鍵是要讓攻擊者花費大量時間通過暴力破解密碼。 – zaph

回答

0

我得到了一個解決我的問題。

對於登錄用戶我剛更改了$ userid ='1';到 $ userid = $ this-> session-> userdata('account_id');

而對於MD5密碼 我只是添加MD5上passwords.Like什麼@sintakonte沒有和@zaph是正確的。

「只能使用強密碼散列算法,如在PHP自己的密碼散列函數中使用的BCrypt。」

參考:https://www.codeigniter.com/userguide3/general/security.html

感謝您的幫助傢伙!

-2

我不打算在這裏討論的MD5主題,但是你應該避免這些弱算法,因爲它們不安全。爲此,使用password_verifypassword_hash。 (但正如我說我不是一個傳教士)

你需要將代碼組織好一點 - 因爲這是一個爛攤子;)

嘗試以下方法 - 控制器

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'; 

     try 
     { 
      $objUser = $this->queries->getUser($userid); 
      if ($objUser->password != md5($cur_password)) throw new Exception('Sorry! Current password is not matching'); 
      if ($new_password != $conf_password) throw new Exception('New password & Confirm password is not matching'); 
      $this->queries->updatePassword($new_password, $userid); 
      echo 'Password updated successfully'; 

     } 
     catch (Exception $e) 
     { 
      echo $e->getMessage(); 
     } 
    } 
    else 
    { 
     echo validation_errors(); 
    } 
} 

和你模型

public function getUser($userid) 
{ 
    $query = $this->db->where(id,$userid])->get('users'); 
    if($query->num_rows() == 1) 
    { 
     return $query->row(); 
    } 
    throw new Exception("no user data found"); 
} 

public function updatePassword($new_password, $userid) 
{ 
    $data = array 
    (
     'password'=> md5($new_password) 
    ); 
    if (!$this->db->where('id', $userid)->update('users', $data)) 
    { 
     throw new Exception('Failed to update password'); 
    } 
} 

沒有必要的模式命名functi在getCurrPassword如果它實際上是返回一個用戶對象 - 所以我重命名了。

+0

不要幫助開發人員提供糟糕的安全性,使用戶處於危險之中。你是否也不是傳教士,幫助某人跳橋?或者在這種情況下,可能會爲成千上萬的用戶提供不存在的安全隱患?當然,這是一個道德問題,優先考慮開發者或所有用戶。注意:這個答案很可能會被數百名未來的開發者諮詢。 – zaph

+0

@sintakonte @sintakonte我試過你提供的代碼,但它總是顯示「找不到用戶數據」,它改變了logged_in用戶的密碼 – squaredsquared

+0

@zaph - 我沒有提供任何東西 - 事實上我說這是一個弱方法,應該根本不會用到 - 但我對此不感興趣,因爲這裏的主題完全不同......(和你跳橋的例子 - 如果你知道死亡不是一種損失 - 但跳線沒有,並想冒險去體驗它 - 我想我不會干涉...) – sintakonte