Take the bank data and compare CODEIGNITER

0

Good afternoon guys,

I was trying to create a login system for a college job when I came across the following:

I am using Codeigniter and MYSQL

I have a user database where you have registered the name (primary key) and a password (with encryption).

I was able to do the insertion of the data in the table everything ok, but when I try to recover this data from the bank to do the login validation it gives me an error.

Basically what I want is: Get the value of just one column from my bank (the password column) that is encrypted with password_hash and compare with the password passed by the user.

My code where I retrieve the value looks like this:

public function loginUsuario($usuario,$senha){
    $this->db->select('*');
    $this->db->from('usuarios');
    $this->db->where('LOGIN',$usuario);
    if($query=$this->db->get()){
        $data = $query->row();
        if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre'){
            return $query->row_array();
        }else{
            return false;
        }
    }

The 'Master password' part is a test password and with it login, I think the error is in the part of:

if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre'){
            return $query->row_array();
        }

Thanks:)

If you need more information leave in the comment.

Thank you for your help.

    
asked by anonymous 26.02.2018 / 17:29

1 answer

0

To get the result in a row the method is result and not row

Change this line:

$data = $query->row();

To:

$data = $query->result();

Complete code:

public function loginUsuario($usuario, $senha) {
    $this->db->select('*');
    $this->db->from('usuarios');
    $this->db->where('LOGIN', $usuario);
    if($query = $this->db->get()) {
        $data = $query->result();
        if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre') {
            return (array)$data;
        } else {
            return false;
        }
    }
}

Notice that I've also changed row_array() to (array)$data , since you already have the data in an object and then I'm transforming it into array. Another alternative would be to use result_array() in the declaration of the variable $data and work with array at all (I prefer the latter).

The row and row_array methods exist, but not when working with Query Builder in CodeIgniter.

row and row_array are methods of the object that is returned with the query method. So:

$query = $this->db->query("YOUR QUERY");

$row = $query->row();
    
26.02.2018 / 18:04