Search code examples
phpcodeigniterphp-password-hash

CodeIgniter - strlen() expects parameter 1 to be string, object given


$query = $this->db->select('password')->get_where('members', array('email' => $this->input->post('email')));

if ($query->num_rows() == 1) {
    if (password_verify($this->input->post('password'), $query->row(1))) {
        return TRUE;
    } else {
        return FALSE;
    }
} else {
    return FALSE;
}

I'm using the above code to verify user input password with hash from the database, and I'm using CodeIgniter which has the built in password_compat to make the functions work for versions less than 5.5.

The above should work, but I keep getting this error:

Severity: Warning Message: strlen() expects parameter 1 to be string, object given Filename: compat/password.php Line Number: 220

What could be the issue here?

Thanks!


Solution

  • You need to get query result in variable and this is wrong use of

    $query->row(1);
    

    It would be

    $rows=$query->row();
    $rows->password;
    

    check below code

    $query = $this->db->select('password')->get_where('members', array('email' => $this->input->post('email')));
    
    if ($query->num_rows() == 1) {
        $rows=$query->row();// here you assign result to $rows variable
    

    Check here how you pass your password variable as parameter

        if (password_verify($this->input->post('password'), $rows->password)) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        return FALSE;
    }
    

    Read Generating Query Results