I'm doing a project with CodeIgniter and I need to encrypt the password.
Controller methods: Login.php:
public function index()
{
$this->form_validation->set_rules('usuario', 'Usuario', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Contraseña', 'trim|required|xss_clean');
if($this->input->post('usuario') != '' && $this->input->post('password') != '')
{
$this->form_validation->set_rules('password', 'Contraseña', 'trim|required|xss_clean|callback_verificar_usuario');
}
if($this->form_validation->run() == FALSE)
{
$data['titulo'] = 'Login';
$data['contenido'] = 'login.php';
$this->load->view('templates/template', $data);
}
else {
redirect(base_url('admin'));
}
}
public function verificar_usuario()
{
$usuario = $this->security->xss_clean($this->input->post('usuario'));
$password = $this->security->xss_clean($this->input->post('password'));
if($this->Login_model->login($usuario, $password))
{
redirect(base_url('admin'));
return true;
}
else {
$this->form_validation->set_message('verificar_usuario', 'Los datos son incorrectos.');
return false;
}
}
Model methods Login_model.php:
public function login($usuario, $password)
{
$this->db->select('usuario, password, rol')
->from('usuarios')
->where('usuario', $usuario)
->where('password', $password);
$result = $this->db->get();
if($result->num_rows() > 0)
{
$row = $result->row();
$data = [
'usuario' => $row->usuario,
'password' => $row->password,
'rol' => $row->rol
];
$this->session->set_userdata($data);
return true;
}
else {
return false;
}
}
In the database, I registered the users with the password_hash()
, and I need in the login, to make the password_verify()
to compare the password that the user enters with the one in the database. How do I achieve that?