Mfdsix Indo
Mfdsix Indo

Reputation: 23

Bcrypt Codeigniter

i have this code on my controller (codeigniter)

this is my login method

function login()
    {
        $post = $this->input->post();
        echo $post['pass_user'];
        if(!empty($post))
        {
            $this->db->where('name_user',$post['name_user']);
            $data = $this->db->get('tb_user')->result_array();
            if(empty($data))
            {
                echo "Tidak ada username";
            }
            else
            {
                if(password_verify($post['pass_user'] , $data['pass_user']))
                {
                    $this->session->set_userdata($data['name_user'], $data['lvl_user']);
                    echo "password same";
                }
                else
                {
                    echo "password not same";
                }
            }
        }
        else
        {
            $this->load->view('login');
        }
    }

and i have value $2y$10$uutShFadO9zEvLMLiHIwcem5hMeFHIG9UQtXeCtKs8ClVJGWZgwSy for my pass_user in my database.

But in result i get 'password not same'. How to solve this ?

Upvotes: 0

Views: 641

Answers (1)

Pradeep
Pradeep

Reputation: 9707

Hope this will help you

You are returning multidimensional data using this result_array(); you have to use it with key in password_verify method like this $data[0]['pass_user'], this will work if you have only single row, So better you should return single array data by using row_array();

Replace this line of code

$data = $this->db->get('tb_user')->result_array();

with this

$data = $this->db->get('tb_user')->row_array();

The whole code

    function login()
    {
        $post = $this->input->post();
        echo $post['pass_user'];
        if(!empty($post))
        {
            $this->db->where('name_user',$post['name_user']);

            //$data = $this->db->get('tb_user')->result_array();

            $data = $this->db->get('tb_user')->row_array();
            if(empty($data))
            {
                echo "Tidak ada username";
            }
            else
            {
                if(password_verify($post['pass_user'] , $data['pass_user']))
                {
                    $this->session->set_userdata($data['name_user'], $data['lvl_user']);
                    echo "password same";
                }
                else
                {
                    echo "password not same";
                }
            }
        }
        else
        {
            $this->load->view('login');
        }
    }

Upvotes: 1

Related Questions