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条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R