Tutorial kali ini akan membahas tutorial authentication login multilevel php dengan framework Codeigniter. Menariknya selain multi level pada admin, juga bisa digunakan untuk login 2 tabel.

Sebelum mengikuti tutorial ini silahkan melakukan setting pada Codeingiter terlebih dahulu lihat postingan sebelumnya di sini.

cari file database.php di application/config/database.php dan tambahkan sintak berikut agar bisa terkoneksi dengan database

'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'db_mydatabase',

NB : Nama database bisa diganti sesuai keinginan

 

1. Buat Table Database

Buatlah 2 tabel yang akan digunakan untuk menampung data hak akses login.

a. Buat Tabel Admin

CREATE TABLE `admin` (
`admin_id` int(11) NOT NULL,
`admin_name` varchar(40) NOT NULL,
`admin_username` varchar(20) NOT NULL,
`admin_password` varchar(50) NOT NULL,
`level` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Masukkan data Admin pada tabel Admin, contoh:

 
admin_id admin_name admin_username admin_password level
1 Andreas Dan dan 827ccb0eea8a706c4c34a16891f84e7b Manager
2 Caroline Diana diana 827ccb0eea8a706c4c34a16891f84e7b Customer Service

b. Buat Tabel Customer

CREATE TABLE `customer` (
`customer_id` int(11) NOT NULL,
`customer_name` varchar(40) NOT NULL,
`customer_username` varchar(20) NOT NULL,
`customer_password` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Masukkan data Customer pada tabel Customer, contoh:

 
admin_id admin_name admin_username admin_password
1 Michael Nakula nakula 827ccb0eea8a706c4c34a16891f84e7b
2 Mark Sadewa sadewa 827ccb0eea8a706c4c34a16891f84e7b

NB : kolom password adalah dengan  hasil fungsi MD5 dari “12345”

2. Views untuk Login

Tulisakan kode berikut ini pada file di dalam folder views. Beri nama page_auth.php

<!doctype html>
<html lang="en">
  <head>
   <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS from Online-->
    <!--<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> -->
    <!-- Bootstrap CSS from Assets Folder-->
    <link rel="stylesheet" href="<?php echo base_url()?>assets/css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-sm-9 col-md-7 col-lg-5 mx-auto">
                <div class="card card-signin my-5">
                    <div class="card-body">
                        <form class="form-signin" method="post" action="<?php echo base_url()?>auth/login">
                          <h1 class="card-title text-center">Login</h1>
                          <br>
                          <?php echo $this->session->flashdata('msg');?>
                          <div class="form-label-group">
                            <input type="text" class="form-control" name="username" placeholder="Username" required autofocus>
                            <label></label>
                          </div>
                          <div class="form-label-group">
                            <input type="password" class="form-control" name="password" placeholder="Password" required autofocus>
                            <label></label>
                          </div>
                          <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

Baca juga :  Instalasi dan Konfigurasi Framework PHP Codeigniter

3. Models untuk Login

Tulisakan kode berikut ini pada file di dalam folder models. Beri nama Dataauth.php

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

class Dataauth extends CI_Model {

    function auth_admin($username,$password){
        $sql = $this->db->query("SELECT * FROM admin WHERE admin_username = '$username' AND admin_password = md5('$password') LIMIT 1");
        return $sql;
    }

    function auth_customer($username,$password){
        $sql = $this->db->query("SELECT * FROM customer WHERE customer_username = '$username' AND customer_password = md5('$password') LIMIT 1");
        return $sql;
    }
}

 

4. Controllers untuk Login

Tulisakan kode berikut ini pada file di dalam folder Controllers. Beri nama Auth.php

<?php
class Auth extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('dataauth');
    }
 
    function index(){
        $this->load->view('page_auth');
    }
 
    function login(){
        $username = htmlspecialchars($this->input->post('username',TRUE),ENT_QUOTES);
        $password = htmlspecialchars($this->input->post('password',TRUE),ENT_QUOTES);
 
        $check_admin = $this->dataauth->auth_admin($username,$password);
        $check_customer = $this->dataauth->auth_customer($username,$password);

        if($check_admin->num_rows() == 1){ //table Admin
            foreach($check_admin->result() as $data){
                $user_data = array(
                    'id' => $data->admin_id,
                    'name' => $data->admin_name,
                    'username' => $data->admin_username,
                    'level' => $data->level
                );
                $this->session->set_userdata($user_data);
            }
            if ($this->session->userdata('level') == 'Manager')
                {
                    redirect('page_manager');
                }
            else if ($this->session->userdata('level') == 'Customer Service')
                {
                    redirect('page_cs');
                }
            else 
                {
                    $text = '<div class="alert alert-danger" role="alert">Kombinasi Username dan Password Salah!</div>';
                    echo $this->session->set_flashdata('msg',$text);
                    redirect('auth');
                }
        }

        elseif($check_customer->num_rows() == 1){ //table Customer
            foreach($check_customer->result() as $data){
                $user_data = array(
                    'id' => $data->customer_id,
                    'name' => $data->customer_name,
                    'username' => $data->customer_username,
                    'level' => 'Customer'
                );
                $this->session->set_userdata($user_data);
            }
            redirect('page_customer');
        }

        else {
            $text = '<div class="alert alert-danger" role="alert">Kombinasi Username dan Password Salah!</div>';
            echo $this->session->set_flashdata('msg',$text);
            redirect('auth');
        }
    }
 
    function logout()
    {
        $this->session->sess_destroy();
        redirect('auth');
    }
}

Sember referensi belajar materi authentication ini beberapa script dari www.fikri.com dan www.malesngoding.com

Baca juga :  Pemrograman PHP Upload Image (Rename)

5. View untuk Dashboard

Tulisakan kode berikut ini pada file di dalam folder views. Beri nama page_user.php

NB : page_user.php akan dipakai bersama oleh 3 Controller, jadi filnya cukup membuat 1 saja.

<!doctype html>
<html lang="en">
  <head>
   <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS from Online-->
    <!--<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> -->
    <!-- Bootstrap CSS from Assets Folder-->
    <link rel="stylesheet" href="<?php echo base_url()?>assets/css/bootstrap.min.css">
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-sm-9 col-md-7 col-lg-5 mx-auto">
                <div class="card card-signin my-5">
                    <div class="card-body">
                        <h3 class="card-title text-center">Welcome to your page!</h3>
                        <ul>
                            <li>Name : <?php echo $this->session->userdata('name') ?></li>
                            <li>Username : <?php echo $this->session->userdata('username') ?></li>
                            <li>Level : <?php echo $this->session->userdata('level') ?></li>
                        </ul>
                        <a href="<?php echo base_url()?>auth/logout">Logout</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

Baca juga :  Pemrograman PHP Upload Image (Rename)

6. Controllers untuk Dashboard

Ada 3 Controller masing-masing untuk 3 user, yaitu halaman manager, customer service, dan customer.

a. Tulisakan kode berikut ini pada file di dalam folder Controllers. Beri nama page_manager.php

<?php
class Page_manager extends CI_Controller{
    function __construct(){
        parent::__construct();

        //user bukan level 'Manager' ditolak
        if ($this->session->userdata('level') !== 'Manager')
            { redirect('auth/logout','refresh');}
    }
 
    function index(){
        $this->load->view('page_user');
    }
}

Lihat script di bawah ini, artinya controller akan me-load view, yaitu page_user.php (tuliskan di script tanpa ekstensi .php)

$this->load->view('page_user');

 

b. Tulisakan kode berikut ini pada file di dalam folder Controllers. Beri nama page_cs.php

<?php
class Page_cs extends CI_Controller{
    function __construct(){
        parent::__construct();

        //user bukan level 'Customer Service' ditolak
        if ($this->session->userdata('level') !== 'Customer Service')
            { redirect('auth/logout','refresh');}
    }
 
    function index(){
        $this->load->view('page_user');
    }
}

 

c. Tulisakan kode berikut ini pada file di dalam folder Controllers. Beri nama page_customer.php

<?php
class Page_customer extends CI_Controller{
    function __construct(){
        parent::__construct();

        //user bukan level 'Customer' ditolak
        if ($this->session->userdata('level') !== 'Customer')
            { redirect('auth/logout','refresh');}
    }
 
    function index(){
        $this->load->view('page_user');
    }
}

 

Di bawah ini Link demo dan download

Admin

  • Level Manager (username : dan | password : 12345)
  • Level Customer Service (username : diana | password : 12345)

Customer

  • Customer 1 (username : nakula | password : 12345)
  • Customer 2 (username : sadewa | password : 12345)

 

Demo Download

 

 

0 CommentsClose Comments

Leave a comment