CodeIgniter Forums
LOGIN WITH CORRECT CREDENTIALS INVOKES VALIDATION ERROR - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: LOGIN WITH CORRECT CREDENTIALS INVOKES VALIDATION ERROR (/thread-59347.html)



LOGIN WITH CORRECT CREDENTIALS INVOKES VALIDATION ERROR - El Forum - 09-23-2013

[eluser]Unknown[/eluser]
Hi,

I have an issue with my login system which invokes a validation error 'Incorrect email/password' even though the credentials are correct. The signUp part works just fine and the records gets posted to the database but logging in with the same credentials becomes an issue. Where could I be possibly going wrong? Below are my view, model and controller.

login_form.php

Code:
<div data-role="page" id="container" data-dom-cache="true">
<div data-role="header" data-theme="a" data-positi>
  <h4><img src="&lt;?php echo base_url(); ?&gt;html/image/logo5.png" /></h4>
</div>&lt;!-- /header --&gt;

<div data-role="content" id="content">
  <div id="login_form">
   <h4>Login</h4>

   &lt;?php
    
    echo validation_errors();
    echo form_open('login/login_validate');
    echo form_input('email_address', 'Email');
    echo form_password('password', 'Password');
    echo form_submit('login_submit', 'Login');
    echo anchor('login/signup', 'Create Account');
    echo form_close();

   ?&gt;

  </div>
</div>&lt;!-- /content --&gt;
<div data-role="footer" data-theme="a">
  <h5>eCamara 2013</h5>
</div>&lt;!-- /footer --&gt;
</div>


institution_users_model.php

Code:
&lt;?php

class Institution_users_model extends CI_Model {

public function validate()
{
  $this->db->where('email_address', $this->input->post('email_address'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('institution_users');
  
  if($query->num_rows == 1){
   return true;
  }
}

public function create_member()
{
  $user_data = array(
   'institution_name' => $this->input->post('institution_name'),
   'email_address' => $this->input->post('email_address'),
   'password' => md5($this->input->post('password'))
   );

  $insert = $this->db->insert('institution_users', $user_data);
  return $insert;
}

}


login. php

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

public function index()
{
  $this->signIn();
}

public function signIn()
{
  $data['main_content'] = 'login_form';
  $data['title'] = 'Login';
  $this->load->view('includes/template', $data);
}

public function signUp()
{
  $data['main_content'] = 'signup_form';
  $data['title'] = 'Sign Up';
  $this->load->view('includes/template', $data);
}

public function members_area()
{
  $is_logged_in = $this->session->userdata('is_logged_in');

  if(!isset($is_logged_in) || $is_logged_in !== true)
  {
   $this->index();
  }
  else
  {
   $data['main_content'] = 'members_area_view';
   $data['title'] = 'Members Area';
   $this->load->view('includes/template', $data);
  }
}

public function login_validate()
{
  $this->form_validation->set_rules('email_address', 'Institution Email', 'trim|required|valid_email|callback_validate_credentials');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
  $query = $this->form_validation->run();
  
  if($query) //if user credentials validate
  {
   $data = array(
    'email_address' => $this->input->post('email_address'),
    'is_logged_in' => true
    );

   $this->session->set_userdata($data);
   redirect('login/members_area');
  }
  else
  {
   $this->index();
  }  
}

public function signUp_validate()
{
  $this->form_validation->set_rules('institution_name', 'Institution Name', 'trim|required');
  $this->form_validation->set_rules('email_address', 'Institution Email', 'trim|required|valid_email|is_unique[institution_users.email_address]');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min-length[6]|max-length[32]|md5');
  $this->form_validation->set_rules('confirm_password', 'Password Confirmation', 'trim|required|matches[password]');
  $signUp_query = $this->form_validation->run();

  if($signUp_query)
  {
   $this->load->model('institution_users_model');
   $create_member = $this->institution_users_model->create_member();

   if($create_member)
   {
    $data['main_content'] = 'signup_successful';
    $data['title'] = 'Account Created';
    $this->load->view('includes/template', $data);
   }
   else
   {
    $this->signUp();
   }
  }
  else
  {
   $this->signUp();
  }

}

public function validate_credentials()
{
  $this->load->model('institution_users_model');

  if($this->institution_users_model->validate() == true)
  {
   return true;
  }
  else
  {
   $this->form_validation->set_message('validate_credentials', 'Incorrect Email/Password');
   return false;
  }
}

public function signOut()
{
  $this->session->sess_destroy();
  $this->index();
}

}

Any assistance would be highly appreciated.
Thanks in advance


LOGIN WITH CORRECT CREDENTIALS INVOKES VALIDATION ERROR - El Forum - 09-23-2013

[eluser]jairoh_[/eluser]
in your model function validate, try using $query->num_rows() instead of $query->num_row and return false if there is no row found. and if still doesn't work try to perform the query in your console maybe there is something wrong with it.


LOGIN WITH CORRECT CREDENTIALS INVOKES VALIDATION ERROR - El Forum - 10-02-2013

[eluser]Unknown[/eluser]
I figured out that the problem was passing the MD5 function in the validation_rules() in the controller and passing the MD5 function again in the model. So I removed the function from the controller but maintained it in the model and Bingo, it worked perfectly!