Web Programming : Meet 15 – 16 (Lanjutan)

Download file disini

Buat library dengan nama Auth.php (dalam folder application/library)

<?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');
  if($this->sesi != TRUE){
      redirect('login','refresh');
      exit();
    }
  }
  
}

Buat Controller Login.php (dalam folder application/controllers)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Login extends CI_Controller {
 
    function __construct(){
    	parent::__construct();
    	$this->load->model('Login_model','login');
    	$this->load->library('form_validation');
    }

    function index()
    {
    	$session = $this->session->userdata('isLogin');
    	if($session == FALSE){
    		$this->load->view('login/form_login');
    	}else{
    		redirect('biodata');
    	}

    }

    function do_login()
    {
    	$username		= $this->input->post("username");
    	$password		= $this->input->post("password");
    	$status_aktif	= $this->input->post("status");

    	$cek = $this->login->cek_user($username,md5($password),$status_aktif);

    	if(count($cek) == 1){
    		foreach ($cek as $cek){
    			$level_cek		= $cek['level'];
    			$user_cek		= $cek['username'];
    			$status_cek		= $cek['status'];
    			$id_cek			= $cek['id'];
    		}

    		$this->session->set_userdata(array(
    			'isLogin'		=> TRUE,
    			'user_data'		=> $user_cek,
    			'level_data'	=> $level_cek,
    			'status_data'	=> $status_cek,
    			'id_data'		=> $id_cek,
    		));
    		redirect('biodata','refresh');
    	}
    	else{
    		$this->session->set_flashdata('message','<div class="alert alert-danger text-center">Gagal Login! / username atau password tidak tepat !!!</div>');
    		redirect(site_url('login'));
    	}

  
    }

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


}

buat file Login_model.php (dalam folder application/models)

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model{
  function __construct()
  {
    parent::__construct();
    $this->tbl = "admin";
  }
 
  function cek_user($username="",$password="",$status_aktif="")
  {
    $query = $this->db->get_where($this->tbl,array('username' => $username, 'password' => $password, 'status' => '1'));
    $query = $query->result_array();
    return $query;
  }

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

    function user($id){
    	$this->db->select('*');
    	$this->db->where('id', $id);
    	$this->db->from('admin');
        return $this->db->get()->row();
    }

}

Dan folder baru dengan nama login dan buat file dengan nama form_login.php (dalam folder application/views/login)

<html>
<head>
  <title>Form 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 alert">
      <?php echo $this->session->flashdata('message');?>
      <form class="form-signin" role="form" action="<?php echo base_url('login/do_login');?>" method="post">
        <h2 class="form-signin-heading"><span class="glyphicon glyphicon-qrcode" aria-hidden="true"></span> Login Form Saya</h2>
        <div class="input-group">
   <span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></span>
   <input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1" name="username">
  </div>
  <br/>
  <div class="input-group">
   <span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></span>
   <input type="Password" class="form-control" placeholder="Password" aria-describedby="basic-addon1" name="password">
  </div>
  <hr>
      <button class="btn btn-lg btn-primary btn-block" type="submit">Masuk</button>
      </form>        
      </div>
      <div class="col-md-4"></div>
    </div>
    </div> <!-- /container -->
  </body>
</html>

 

Recommended For You

About the Author: adyrhs

Leave a Reply

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