CodeIgniter Forums
Using 1 method for authentication and form callback HELP - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using 1 method for authentication and form callback HELP (/showthread.php?tid=47411)



Using 1 method for authentication and form callback HELP - El Forum - 12-07-2011

[eluser]Unknown[/eluser]
I would like to consolidate my validate method and my login method so my users only see http://mysite.com/user/login. Also, the form validation callback is not working.

Can someone please help me clean up this code?

Thank you.


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

class User extends CI_Controller {

function __construct() {
  parent::__construct();
  $this->load->model('user_model');
}

function login() {
  $this->load->view('login_view', $data);
}

function validate() {
  $username=$this->input->post('username');
  $password=$this->input->post('password');
  
  $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[16]|callback_login_check');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[16]|callback_login_check');
  
  if ($this->form_validation->run()) {
   if($this->user_model->check_user($username, $password)) {
    $data=array(
      'username'=>$username,
      'is_logged_in'=>TRUE,
      'user_id'=>$this->user_model->get_user_id($username)
    );
    $this->session->set_userdata($data);
    redirect('home');
   } else {
    $this->form_validation->set_message('login_check', 'Invalid username/password combination!');
    $this->login();
   }
  } else {
  $this->form_validation->set_message('login_check', 'Invalid username/password combination!');
  $this->login();
  }
}