I have a table for users where the data for the password column is encoded using the encrypt library in Codeigniter. Now, I want to select encoded column to decode and compare with the password's user input (Login Validation). Here's the code. I inserted the value like this:
$this->db->insert("my_table",Array("username"=>$this->input->post("username"),"password"=>$this->encrypt->encode($this->input->post("password"))));
For now I validate the input this way:
$data = $this->db->get("mytable");
foreach($data as $d){
if($d["username"] == $this->input->post("username") && $d["password"] == $this->encrypt->decode($this->input->post("password")){
//success
break;
}
}
This works so fine to me but, I want a shorter and cleaner way to do this. You know, also, for future coding practice. Here's what I have done so far:
$this->db->get_where("my_table",Array($this->encrypt->decode("password")=>$this->input->post("password")));
But Yeah! This returns an error message. The error says:
Unknown column '0' on where clause
The problem is that you are setting decoded password as the database column.
Array($this->encrypt->decode("password")=>$this->input->post("password"))
What you need to be doing is something more like:
Array("password" => $this->encrypt->decode("password"))
First of all, is NOT a good idea to encrypt a password if can be decrypted!!!
And best practice is to encrypt input and then to select in database
$this->db->select('*')
$yhis->db->from('mytable')
$this->db->where('username', $this->input->post("username"));
$this->db->where('password', $this->encrypt->encode($this->input->post("password")));
$this->db->get();
if result... do login