0

The following function is not returning true and I do not understand why. I'm not getting any database errors, but it definitely returns false for some reason. The table and field names are 100% correct.

public function verify_password($username, $password){
    $this->db->select('password');
    $this->db->from('user_account');
    $this->db->where('username', $username);
    $query = $this->db->get();

    if($query == $password){
      return true;
    } else {
      return false;
    }
  }
2
  • if($query->row(0) == $password) Commented Apr 19, 2018 at 19:28
  • 3
    Don't store plain text passwords. Instead use password_hash() and password_verify(). Commented Apr 19, 2018 at 19:29

1 Answer 1

4

The problem is that you need to get some results from your query. Read about Generating Query Results.

Revised method as follows

public function verify_password($username, $password)
{
    //use method chaining, it's more efficient and less typing
    $query = $this->db
        ->select('password')
        ->from('user_account')
        ->where('username', $username)
        ->get();

    //were any matching rows found?
    if($query->num_rows() > 0)
    {
        // get first row of data and check 'password' value against passed value
        // return is a boolean
        return $query->row()->password === $password;
    }

    //There are no rows that match so clearly not logged in
    return false;
}

As the comments in your question point out you should not store plain text passwords.

Sign up to request clarification or add additional context in comments.

2 Comments

I think he is storing plain passwords.
It was the way that I was trying to access the query result to compare with the password that was the issue. And yes I understand they should not be stored as plaintext, I will get round to that eventually. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.