Login system using CodeIgniter

Published on : June 3, 2014

Author:

Category: CodeIgniter


We already learned CodeIgniter basic theming, professional theming, data insert, reporting. Today we will learn Login with CodeIgniter.

Create a table in the name “login” with the following code. After creating table we will manually input a data row.

CREATE TABLE `login` (
   `id` int(10) unsigned NOT NULL auto_increment,
   `user_name` varchar(50) collate latin1_general_ci NOT NULL,
   `password` varchar(50) collate latin1_general_ci NOT NULL,
   PRIMARY KEY (`id`)
);

Now create a page name “home.php” with the following code.

<?=form_open('login/login_submit/'); ?>
   <table width="485" cellspacing="0" cellpadding="0">
      <tr>
         <td width="85"></td>
         <td width="200" colspan="2">Error message will print here</td>
      </tr>
      <tr>
         <td>Email:</td>
         <td><input name="email" value="" type="text" /></td>
         <td>Email, Error message</td>
      </tr>
      <tr valign="top">
         <td>Password:</td>
         <td><input name="password_login" type="password" /></td>
         <td>Password, Error message</td>
      </tr>
      <tr>
         <td width="223">&nbsp;</td>
         <td><input name="submit" type="submit" value="Login" alt="login" /></td>
      </tr>
   </table>
<?=form_close(); ?>

 

Our form will submit to “’login/login_submit/”. That’s mean our controller name “login.php” and it has a function named “login_submit”.

So, first create a controller in the name “login.php” with the following code.

class Login extends CI_Controller
{
  function __construct()
  {
     parent::__construct();
     $this->load->helper(array('form', 'url', 'html'));
     $this->load->library('form_validation');
     $this->form_validation->set_message('alpha', 'Invalid Name');
     $this->form_validation->set_message('valid_email', 'Invalid Email Address');
     $this->form_validation->set_message('required', 'Require');
     $this->form_validation->set_message('matches', 'Password does not match');
  }
}

Here we just declared a construct. As, our login information is stored in DB. We need to declare a model.

Create a model in the name “login_model.php” with following code.

class login_model extends CI_Model
{
   //========================= MODEL CONSTRUCTOR =================
   function __construct()
   {
      parent::__construct();
   }
   //======================== LOGIN INFO =========================
   function login()
   {
      $this->db->select('user_name, password');
      $this->db->from('login');
      $this->db->where('user_name', $this->input->post('email'));
      $this->db->where('password', $this->input->post('password_login'));
      $query = $this->db->get();
      return $query->result();
      $this->db->close();
   }
}

Now load medel from controller. Update controller with following code

class Login extends CI_Controller
{
  function __construct()
  {
     parent::__construct();
     $this->load->helper(array('form', 'url', 'html'));
     $this->load->library('form_validation');
     $this->form_validation->set_message('alpha', 'Invalid Name');
     $this->form_validation->set_message('valid_email', 'Invalid Email Address');
     $this->form_validation->set_message('required', 'Require');
     $this->form_validation->set_message('matches', 'Password does not match');

     $this->load->model('login_model');

  }
}

Now, create “login_submit” function into controller. Code like below.

class Login extends CI_Controller
{

  function __construct()
  {
     parent::__construct();
     $this->load->helper(array('form', 'url', 'html'));
     $this->load->library('form_validation');
     $this->form_validation->set_message('alpha', 'Invalid Name');
     $this->form_validation->set_message('valid_email', 'Invalid Email Address');
     $this->form_validation->set_message('required', 'Require');
     $this->form_validation->set_message('matches', 'Password does not match');

     $this->load->model('login_model');
  }

  /********************* LOGIN INFO *********************/
  public function login_submit($page = 'home')
  {
     if ( ! file_exists('application/views/pages/'.$page.'.php'))
     {
        show_404();
     }
     $data['title'] = ucfirst($page);
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
     $this->form_validation->set_rules('password_login', 'Login Password', 'required');
     if ($this->form_validation->run() == FALSE)
     {
        $this->load->view('templates/header', $data);
        $this->load->view('pages/home',$data);
        $this->load->view('templates/footer', $data);
     }
     else
     {
        $data['query'] = $this->login_model->login();
        if($data['query'] != NULL)
        {
           $data_se = array(
              'username' => $this->input->post('email'),
              'is_logged_in' => true
           );
           $this->session->set_userdata($data_se);
           $data['invalid_user_pass'] = 'Logged In';
           $this->load->view('templates/header', $data);
           $this->load->view('pages/home',$data);
           $this->load->view('templates/footer', $data);
        }
        else
        {
           $data['invalid_user_pass'] = 'Invalid user name or password';
           $this->load->view('templates/header', $data);
           $this->load->view('pages/home',$data);
           $this->load->view('templates/footer', $data);
        }
     }
  }
}

Finally, update your form with following code

<?=form_open('login/login_submit/'); ?>
   <table width="485" cellspacing="0" cellpadding="0">
      <tr>
         <td width="85"></td>
         <td width="200" colspan="2"><?php if(isset($invalid_user_pass)){echo $invalid_user_pass;}?></td>
      </tr>
      <tr>
         <td>Email:</td>
         <td><input name="email" value="<?=set_value('email');?>" type="text" /></td>
         <td><?=form_error('email');?></td>
      </tr>
      <tr valign="top">
         <td>Password:</td>
         <td><input name="password_login" type="password" /></td>
         <td><?=form_error('password_login');?></td>
      </tr>
      <tr>
         <td width="223">&nbsp;</td>
         <td><input name="submit" type="submit" value="Login" alt="login" /></td>
      </tr>
   </table>
<?=form_close(); ?>

 

Now visit – http://localhost/your_root_folder/index.php/controllers_name/controllers_function_name

Example : http://localhost/CodeIgniter/index.php/login/ login_submit


Leave a Reply

Your email address will not be published. Required fields are marked *