Membuat Antrian Dengan CodeIgniter dan Bootstrap (Part 5) Membuat Session Login

Assalamu’alaikum wr. wb.

“Membuat Antrian Dengan CodeIgniter dan Bootstrap (Part 5) Membuat Session Login”

sebelumnya telah dibahas sampai mencetak nomor antrian pada Part 4. selanjutnya akan dibahas bagaimana membuat daftar pemanggilan nomor antrian dengan jquery dan function setinterval

Update tabel ambil_antrian menjadi seperti ini

buat tabel baru dengan nama “user”

CREATE TABLE `user` (
  `u_id` int(2) NOT NULL,
  `u_username` varchar(20) DEFAULT NULL,
  `u_password` varchar(50) DEFAULT NULL,
  `u_jenis` int(1) DEFAULT NULL COMMENT 'untuk loket CS'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`u_id`, `u_username`, `u_password`, `u_jenis`) VALUES
(1, 'cs1', 'e10adc3949ba59abbe56e057f20f883e', 1),
(2, 'cs2', 'e10adc3949ba59abbe56e057f20f883e', 2);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user`
--
ALTER TABLE `user`
  ADD PRIMARY KEY (`u_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
  MODIFY `u_id` int(2) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

selanjutnya buat sebuah file baru didalam folder “libraries” lokasi folder /application/libraries/……. buat file dengan nama “Auth.php”

file Auth.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth {
    public function cek_auth()
  {
    $this->ci =& get_instance();
    $this->ci->session->userdata('status') == 1;
    $this->sesi   = $this->ci->session->userdata('isLogin');
    $this->hak 	  = $this->ci->session->userdata('stat');
    //$this->status = $this->ci->session->userdata('status');
    if($this->sesi != TRUE){
      redirect('login','refresh');
      exit();
    }
  }
}

selanjutnya update file autoload.php yang berada didalam folder config, tambahkan libraries ‘auth’ agar librari ini bisa di-load secara otomatis

selanjutnya buat sebuah controller baru dengan nama Login.php (controllers/Login.php)

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

class Login extends CI_Controller {

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

    function index()
    {
    	$session = $this->session->userdata('isLogin');

        $data = array(
            'login' => site_url('login/do_login'),
        );

    	if($session == FALSE){
    		$this->load->view('login/v_login',$data);
    	}else{
        echo "<script>
        alert('Anda Login sebagai : ".$this->session->userdata('user_data')."');
        window.location.href='".site_url('home/index')."';
        </script>";
    	}

    }

    function do_login()
    {
    	$user		= $this->input->post("u_username",TRUE);
    	$pass		= $this->input->post("u_password",TRUE);

    	$cek = $this->login->cek_user($user,md5($pass));

    	if(count($cek) == 1){
    		foreach ($cek as $cek){
    			$jenis_cek	= $cek['u_jenis'];
          $user_cek		= $cek['u_username'];
    			$id_cek			= $cek['u_id'];
            }

    		$this->session->set_userdata(array(
    			'isLogin'		  => TRUE,
          'jenis_data'  => $jenis_cek,
    			'user_data'   => $user_cek,
          'id_data'		  => $id_cek,
    		));
        echo "<script>
        alert('Anda Login sebagai : ".$this->session->userdata('user_data')."');
        window.location.href='".site_url('home/index')."';
        </script>";

    	}
    	else{
    		echo "<script>
        alert('Username dan Password Tidak Tepat');
        window.location.href='".site_url('login')."';
        </script>";
    	}
    }

    function logout(){
    	$this->session->sess_destroy();
    	redirect('login','refresh');
    }


}

kemudian models baru dengan nama M_login.php (models/M_login.php)

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_login extends CI_Model{
  function __construct()
  {
    parent::__construct();
  }

  function cek_user($username="",$password="")
  {
    $query = $this->db->get_where('user',array(
            'u_username' => $username,
            'u_password' => $password,
        ));
    $query = $query->result_array();
    return $query;
  }

  function ambil_user($username)
        {
        $query = $this->db->query("SELECT * FROM user WHERE u_username='$username' ");
        $query = $query->result_array();
        if($query){
            return $query[0];
        }
    }

}

selanjutnya membuat folder “login” dan file “v_login.php” baru didalam folder views (views/login/v_login.php)

<html>
<head>
  <title>Sistem Antrian | Login</title>
  <link rel="stylesheet" href="<?php echo base_url();?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
  <div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4">
      <h1>Halaman Login</h1>
      <form action="<?php echo $login;?>" method="post">
        <label>Username</label>
        <input type="text" name="u_username" class="form-control" required="required">
        <br/>
        <label>Username</label>
        <input type="password" name="u_password" class="form-control" required="required">
        <br/>
        <button type="submit" class="btn btn-danger">Login</button>
      </form>
    </div>
    <div class="col-md-4"></div>
  </div>
</div>
</body>
</html>

oke. sampai disini dulu, untuk pembuatan session login, nanti akan dilanjutkan untuk menampilkan data antrian yang selanjutnya akan diproses untuk panggil antrian

Recommended For You

About the Author: adyrhs

Leave a Reply

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