I Have the following class to validate the user Login,
class Validation_model extends CI_Model {
public function validateLogin($userid,$userpass){
$this->load->library('encrypt');
$this->load->database();
$encrypted_string = $this->encrypt->encode($userpass);
$this->db->select('*');
$this->db->from('ecom-user');
$this->db->where('userid', $userid);
$this->db->where('userpassword', $encrypted_string);
$result = $this->db->get();
echo $result->num_rows();
}
}
But when even I entered a valid userid & password it returns the row count as 0, Please help me with this, where I have done the mistake?
It does not work because you are encrypting the password.
The encryption function used by CodeIgniter uses a random initialization vector (IV) every time it encrypts something, which means that the cipher-text (the encrypted text) will be different every time it is encrypted.
When dealing with passwords you need to hash them, not encrypt them. They are related but they are not the same thing.
If you have PHP >=5.5.0 then you can do:
$hashed_password = password_hash($userpass, PASSWORD_DEFAULT);
// ...
$this->db->where('userpassword', $hashed_password);
For this to work you also need to re-hash the passwords in your database.