nadiakrtika
nadiakrtika

Reputation: 13

change password on codeigniter

I tried to make a change password function, but nothing happened.I actually adapted codeigniter change password code but again, nothing happened.Here I attached the code, any hep would be appreciated.

the controller -

function change_password_process()
    {
        $this->load->view('ubahpassword_view');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->form_validation->set_rules('pass_lama','Password Lama','trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('pass_baru','Password Baru','trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('ulangpass_baru','Ulangi Password Baru','trim|required|min_length[4]|max_length[32]|matches[pass_baru]');

        if ($this->form_validation->run() == FALSE)
        {
            redirect('ubahpassword');       
        }
        else 
        {
            $query = $this->rekammedis_model->change_password();
            redirect('ubahpassword');
        }
    }

The model -

function change_password()
    {   
        $this->db->select('id');
        $this->db->where('username', $this->session->userdata('username'));
        $this->db->where('id', $this->session->userdata('id'));
        $this->db->where('password', md5($this->input->post('pass_lama')));
        $query = $this->db->get('user');   

        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            if($row->id === $this->session->userdata('id'))
            {
                $data = array(
                  'password' => md5($this->input->post('pass_lama'))
                );

                $this->db->where('username', $this->session->userdata('username'));
                $this->db->where('password', md5($this->input->post('pass_lama')));

                    if($this->db->update('user', $data)) 
                    {
                        return "Password berhasil diganti!";
                    }

                    else
                    {
                        return "Terdapat kesalahan, password tidak terganti";
                    }
            }

            else
            {
                return "Terdapat kesalahan, password tidak terganti";
            }


        }

        else
        {
            return "Password lama salah";
        }

    }

Upvotes: 1

Views: 726

Answers (1)

Varun Garg
Varun Garg

Reputation: 2654

Assuming baru means new and lama means old.

You were changing your old password with old password itself (pass_baru)

So Replace

 $data = array('password' => md5($this->input->post('pass_lama')));

with

 $data = array('password' => md5($this->input->post('pass_baru')));


UPDATE
Found Another Bug

if ($this->form_validation->run() == FALSE)
{
    redirect('ubahpassword');    
}

You can't redirect it, if you do, you won't get validated here.
Load your view here itself. Also update your code above if it does not work

Upvotes: 1

Related Questions