dtzhfb4869 2017-09-14 08:40
浏览 217
已采纳

Codeigniter:更改登录用户的密码和使用md5的密码

I want to ask how to change password for logged in user, I can change the password when I enter password that matches anyone in database. For just example, user have "admin" password, I just enter the current password, new password and confirm password.

Current password: admin New Password: newadmin Current Password: new admin

And also I don't know how to change the password if the password uses md5(). I hope you can help me, I am a newbie on Codeigniter. I search answers but I really don't understand it so I want to comment but it's required 50 reputation so I post new question.

Here's my code :

Controller

    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();
}

model

 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); }
  • 写回答

2条回答 默认 最新

  • dqrzot2791 2017-09-21 07:31
    关注

    I got a solution for my problem.

    For logged in user I just changed the $userid = '1'; into $userid = $this->session->userdata('account_id');

    And for the md5 password I just add md5 on passwords.Like what @sintakonte did and @zaph is right.

    "Only use strong password hashing algorithms like BCrypt, which is used in PHP’s own Password Hashing functions."

    Reference : https://www.codeigniter.com/userguide3/general/security.html

    Thanks for the help guys!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分