2017-09-14 133 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