In this tutorial I will show you User Login system in Codeigniter. After login success redirect to another page and set a user id value in a session variable in codeigniter web application.
I will show you with process and example with source code in this tutorial.
Process:
1) Create a login form,
2) Set login action in form and go to controller page,
3) In controller page use some helper, like form helper, URL and session library for use session,
4) Define a model method in controller page and go to Model page,
5) In model page match you form value with the MySQL table data
6) After login success set redirect page and ser value in a session variable.
7) If login failed so give a error message use session flash message.
Example and Source Code:
Form:
<?php $fcl=array('class'=>'form-login'); echo form_open('Controller/user_login',$fcl); ?> <div class="form-group"> <input type="email" class="form-control" name="username" placeholder="Username"> </div> <div class="form-group form-actions"> <input type="password" class="form-control password" name="password" placeholder="Password"> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary"> Login </button> </div> <?php echo form_close(); ?>
Controller:
<?php class Controller extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->helper('form'); $this->load->library('session'); $this->load->database(); $this->load->model('Model'); } // Login Chack // public function user_login(){ $this->User->userLogin(); } } ?>
Model:
<?php function userLogin(){ $this->db->where('email',$this->input->post('username')); $this->db->where('password',$this->input->post('password')); $sql=$this->db->get('login'); $r_cou=$sql->num_rows(); if($r_cou==1){ $this->session->set_userdata('user_id',$this->input->post('username')); redirect("dashboard"); } else{ $this->session->set_flashdata('category_error', 'Your Email Id And Password Dismatch. Please Enter Again'); redirect("login"); } } ?>
Or try it,
<?php function userLogin(){ $sql=$this->db->where('email',$this->input->post('username')) ->where('password',md5($this->input->post('password')))->get('login'); $r_cou=$sql->num_rows(); if($r_cou==1){ $this->session->set_userdata('user_id',$this->input->post('username')); redirect("dashboard"); } else{ $this->session->set_flashdata('category_error', 'Your Email Id And Password Dismatch. Please Enter Again'); redirect("login"); } }