Sunday, 15 November 2015

How to create login and logout.

View:
<center> <?php echo $this->session->flashdata('error_message_name');?>  </center>
<form class="form col-md-12 center-block" action="<?php echo base_url();?>admin/login/validate_credentails" method="post" name="login-form">
          <div class="form-group">
            <input type="text" id="username" name="username" class="form-control" placeholder="User Name"  autofocus="autofocus" required="required">
          </div>
          <div class="form-group">
            <input type="password" id="password" name="password" class="form-control" placeholder="Password"  required="required">
          </div>
          <div class="form-group">
            <input type="submit" value="Sign In" name="submit" class="btn btn-primary btn-lg btn-block">
            <span class="pull-right"> <?php echo anchor('admin/register','Register');?></span> </div>
        </form>
********************************************************************************

Controller:

  public function lcheck() {

        $this->load->database();     

        if ($this->session->userdata('is_login')) {
            redirect('contact');
        } else {
              $user = $_POST['username'];
            $password = $_POST['password'];
           
                $this->db->select('*');
                $this->db->from('tbl_users');
                $this->db->where('user_name',$user);
                $this->db->where('user_password',$password);                               
                $val=$this->db->get();
               
                if ($val->num_rows()>0) {
                    foreach ($val->result_array() as $recs => $res) {
                        $this->session->set_userdata(array(
                            'id' => $res['user_id'],
                            'username' => $res['user_name'],                           
                            'useremail' => $res['user_email'],                                                       
                            'is_login' => true)
                        );
                    }
                    redirect('dashboard');
                   
                } else {   
            $this->session->set_flashdata('success_message', 'Error in username or password.');              
            redirect('login');
                }
         
        }
 }

  public function logout() {
        $this->session->unset_userdata('user_id');
        $this->session->unset_userdata('user_name');
        $this->session->unset_userdata('is_admin_login');  
        $this->session->sess_destroy();
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
        redirect('login', 'refresh');
    }

No comments:

Post a Comment