<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login/login_form');
}
}
public function proceed()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|max_length[150]|valid_email|callback_check_user');
$this->form_validation->set_rules('pass', 'Password', 'required|max_length[50]|md5');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login/login_form');
}
else
{
$this->load->library('session');
$newdata = array(
'email' => $this->input->post('email'),
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
//write login time, ip and increment login count to db
$this->load->database();
$sql2 = "UPDATE users SET ip_address = ?, login_count = login_count + 1 WHERE email = ?";
$query2 = $this->db->query($sql2, array($this->input->ip_address(), $this->input->post('email')));
if ($query2) {
redirect('/index.php/home', 'location');
}
else {
log_message('error', 'User login time, ip and login counts were not updated to db.');
redirect('/index.php/login', 'location');
}
}
}
function check_user($str)
{
$this->load->database();
$sql = "SELECT id, registered FROM users WHERE email = ? and password = ?";
$query = $this->db->query($sql, array($this->input->post('email'), $this->input->post('pass')));
if ($query->num_rows() > 0)
{
$row = $query->row();
if ($row->registered == 1) {
//generate cookie and redirect to home page
return TRUE;
}
else {
$this->form_validation->set_message('check_user', 'Sorry, your registration has not been confirmed yet. Please <a >click here</a> if you did not receive the confirmation email.');
return FALSE;
}
}
else
{
$this->form_validation->set_message('check_user', 'Sorry, user does not exist. Please try again.');
return FALSE;
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
The line that i’m having problems with is this:
$query = $this->db->query($sql, array($this->input->post('email'), $this->input->post('pass')));
The function “check_user” always returns FALSE however if I replace the
$this->input->post('pass')with the actual hashed password from the database, the function returns TRUE and it works!
Any idea where I messed up would be greatly appreciated. Thanks!