Membuat Login ,session and multi login with adminLTE




PART 1
MEMBUAT HALAMAN LOGIN

Route.php



$route['default_controller'] = "Login";
$route['404_override'] = '';


controller/login.php


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

class Login extends CI_Controller {

public function index()
{
$this->load->view('p_login');
}
}

view/login

<!Doctype html>
<html>
<head>
<title>Halman Login </title>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
   folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>dist/css/skins/_all-skins.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/iCheck/flat/blue.css">
  <!-- Morris chart -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/morris/morris.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/jvectormap/jquery-jvectormap-1.2.2.css">
  <!-- Date Picker -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/datepicker/datepicker3.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/daterangepicker/daterangepicker.css">
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
<style>
.loginbox{
margin:180px auto; 
width:450px;
position:relative;
border-radius:15px;
background:#fffff0f0;
}
body{
background-color:rgb(209,209,209);
}
</style>
</head>
<body>
<!-- Horizontal Form -->
<div class="box box-info loginbox">
            <div class="box-header with-border">
              <h3 class="box-title">Horizontal Form</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form class="form-horizontal">
              <div class="box-body">
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-2 control-label">Username</label>

                  <div class="col-sm-10">
                    <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">Password</label>

                  <div class="col-sm-10">
                    <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                      <label>
                        <input type="checkbox"> Remember me
                      </label>
                    </div>
                  </div>
                </div>
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="submit" class="btn btn-default">Cancel</button>
                <button type="submit" class="btn btn-info pull-right">Sign in</button>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>

</body>
</html>




PART 2
MENENTUKAN HALAMAN ADMIN DAN USER
TAMPA DATABASE

controller/login.php


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

class Login extends CI_Controller {

public function index()
{
$this->load->view('p_login');
}
}



controller/admin


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

class Admin extends CI_Controller {

public function index()
{
$this->load->view('p_admin');
}
}


controller/user


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

class User extends CI_Controller {

public function index()
{
$this->load->view('p_user');
}
}



p_user.php

<!Doctype html>
<html>
<head>
<title></title>
</head>
<body>
This is page user
</body>

</html>

p_admin.php

<!Doctype html>
<html>
<head>
<title></title>
</head>
<body>
This is page Admin
</body>

</html>

config/autoload.php

$autoload['model'] = array('db_model');


config/route.php

$route['default_controller'] = "Login";

$route['404_override'] = '';


.htaccess

RewriteEngine on
RewriteBase /belajaradmin
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1[L,QSA]


view/p_login.php


<!Doctype html>
<html>
<head>
<title>Halman Login </title>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
   folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>dist/css/skins/_all-skins.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/iCheck/flat/blue.css">
  <!-- Morris chart -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/morris/morris.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/jvectormap/jquery-jvectormap-1.2.2.css">
  <!-- Date Picker -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/datepicker/datepicker3.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/daterangepicker/daterangepicker.css">
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
<style>
.loginbox{
margin:180px auto;
width:450px;
position:relative;
border-radius:15px;
background:#fffff0f0;
}
body{
background-color:rgb(209,209,209);
}
</style>
</head>
<body>
<!-- Horizontal Form -->
<div class="box box-info loginbox">
            <div class="box-header with-border">
              <h3 class="box-title">Login Form</h3>
            </div>
<?php
if(isset($_POST['masuk'])){
$u=$this->input->post('usr');
$p=$this->input->post('pwd');
if($u=='admin' && $u=='admin'){
header('location:'.base_url().'admin');
}else if($u=='user' && $u=='user'){
header('location:'.base_url().'user');
}
}

?>
            <!-- /.box-header -->
            <!-- form start -->
            <form class="form-horizontal" method="post" action="">
              <div class="box-body">
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-2 control-label">Username</label>

                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="inputEmail3" name="usr" placeholder="Email">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">Password</label>

                  <div class="col-sm-10">
                    <input type="password" class="form-control" id="inputPassword3" name="pwd" placeholder="Password">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                      <label>
                        <input type="checkbox"> Remember me
                      </label>
                    </div>
                  </div>
                </div>
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="reset" class="btn btn-default">Cancel</button>
                <button type="submit" name="masuk" class="btn btn-info pull-right">Sign in</button>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>

</body>

</html>

GET VIDEO OR DOWNLOAD



PART 3

MENENTUKAN HALAMAN 
USER DAN ADMIN MELALUI DATABASE 
DISERTAI SESSIONNYA SAAT LOGIN


controller/login.php

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

class Login extends CI_Controller {

public function _construct(){
session_start();
}
public function index()
{
$cek=$this->session->userdata('username');
if(empty($cek)){
$this->load->view('p_login');
}else{
$st=$this->session->userdata('stts');
if($st=='admin'){
header('location:'.base_url().'admin');
}else{
header('location:'.base_url().'user');
}
}
}
}

controller/logout.php


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

class Logout extends CI_Controller {


public function index()
{
$cek=$this->session->userdata('username');
if(empty($cek)){
header('location:'.base_url());
}else{
$this->session->sess_destroy();
header('location:'.base_url());
}
}
}


controller/admin.php

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

class Admin extends CI_Controller {

public function index()
{
//untuk sesi login
$cek=$this->session->userdata('username');
if($cek=='admin'){
$this->load->view('p_admin');
}else{
header('location:'.base_url());
}

}
}


controller/user.php


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

class User extends CI_Controller {

public function index()
{
//untuk sesi login
$cek=$this->session->userdata('username');
if($cek=='user'){
$this->load->view('p_user');
}else{
header('location:'.base_url());
}
}
}



config/database



$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_belajaradmin';

$db['default']['dbdriver'] = 'mysql';


models/db_model.php

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

class Db_model extends CI_Model {
public function getLoginData($usr,$pwd){
$u=mysql_real_escape_string($usr);
$p=md5(mysql_real_escape_string($pwd));
$cek_login = $this->db->get_where('tb_users',array('username'=>$u,'password'=>$p));
if($cek_login->num_rows()>0){
$qad=$cek_login->row();
if($u == $qad->username && $p == $qad->password){
$sess=array(
'username'=>$qad->username,
'stts'=>$qad->status
);
$this->session->set_userdata($sess);
if($qad->status=='admin'){
header('location:'.base_url().'admin');
}else{
header('location:'.base_url().'user');
}

}

}else{
echo"<script>alert('username or password salah ,silahkan coba lagi ,,,,');";
echo"window.location.href='".base_url()."';";
echo"</script>";
}
}
}


view/p_login.php


<!Doctype html>
<html>
<head>
<title>Halman Login </title>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
   folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>dist/css/skins/_all-skins.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/iCheck/flat/blue.css">
  <!-- Morris chart -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/morris/morris.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/jvectormap/jquery-jvectormap-1.2.2.css">
  <!-- Date Picker -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/datepicker/datepicker3.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/daterangepicker/daterangepicker.css">
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="<?php echo base_url()."assets/"?>plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
<style>
.loginbox{
margin:180px auto;
width:450px;
position:relative;
border-radius:15px;
background:#fffff0f0;
}
body{
background-color:rgb(209,209,209);
}
</style>
</head>
<body>
<!-- Horizontal Form -->
<div class="box box-info loginbox">
            <div class="box-header with-border">
              <h3 class="box-title">Login Form</h3>
            </div>
<?php
if(isset($_POST['masuk']))
{
$u=$this->input->post('usr');
$p=$this->input->post('pwd');
$this->db_model->getLoginData($u,$p);
}


?>
            <!-- /.box-header -->
            <!-- form start -->
            <form class="form-horizontal" method="post" action="">
              <div class="box-body">
                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-2 control-label">Username</label>

                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="inputEmail3" name="usr" placeholder="Email">
                  </div>
                </div>
                <div class="form-group">
                  <label for="inputPassword3" class="col-sm-2 control-label">Password</label>

                  <div class="col-sm-10">
                    <input type="password" class="form-control" id="inputPassword3" name="pwd" placeholder="Password">
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-sm-offset-2 col-sm-10">
                    <div class="checkbox">
                      <label>
                        <input type="checkbox"> Remember me
                      </label>
                    </div>
                  </div>
                </div>
              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <button type="reset" class="btn btn-default">Cancel</button>
                <button type="submit" name="masuk" class="btn btn-info pull-right">Sign in</button>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>

</body>
</html>



GET VIDEO OR DOWNLOAD












Membuat Login ,session and multi login with adminLTE Membuat Login ,session and multi login with adminLTE Reviewed by Leo on 21:40 Rating: 5

No comments