Multi-level User Login From Different Table Using Codeigniter 3

https://cdn.elegantthemes.com/blog/wp-content/uploads/2014/01/user-roles-thumb.jpg

Pada kesempatan ini saya mau share bagaimana cara membuat hak akses user pada CodeIgniter dengan menggunakan banyak tabel. Sebagai contoh disini saya membuat dua buah tabel yaitu Admin dan Member.

Disini saya hanya akan menjelaskan point-point pentingnya saja, sisanya silahkan download source code yang sudah di sediakan di bawah.

Tabel Admin

CREATE TABLE `admin` (
  `admin_id` int(10) NOT NULL,
  `admin_user` char(30) NOT NULL,
  `admin_pass` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `admin`
  ADD PRIMARY KEY (`admin_id`),
  ADD UNIQUE KEY `admin_user` (`admin_user`);

 Tabel Member

CREATE TABLE `member` (
  `member_id` int(10) NOT NULL,
  `member_user` char(20) NOT NULL,
  `member_pass` varchar(255) NOT NULL,
  `member_nama` varchar(255) NOT NULL,
  `member_alamat` text NOT NULL,
  `member_ttl` date NOT NULL,
  `member_email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `member`
  ADD PRIMARY KEY (`member_id`),
  ADD UNIQUE KEY `member_user` (`member_user`);

Buat file Controller Utama - C_Main

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class C_Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('M_Main');
}
public function index()
{
//Check session
if($this->session->userdata('admin_user'))
{
$this->load->view('admin/V_dashboard');
}
elseif($this->session->userdata('member_user'))
{
$this->load->view('member/V_dashboard');
}
else
{
$this->load->view('V_Login');
}
}
public function login()
{
$username  = $this->input->post('username');
$password  = md5($this->input->post('password'));
$cek_admin = $this->M_Main->get_admin($username,$password);
$cek_member = $this->M_Main->get_member($username,$password);
if($cek_admin->num_rows() == 1)
{
foreach($cek_admin->result_array() as $row)
{
$pass_auth = $row['admin_pass'];
if($password == $pass_auth)
{
$row_data = array(
'admin_id' => $row['admin_id'],
'admin_user' => $row['admin_user']
);
$this->session->set_userdata($row_data);
redirect('admin/C_Admin');
}
else
{
//if wrong password
$this->load->view('V_Login');
}
}
}
elseif($cek_member->num_rows() == 1)
{
foreach($cek_member->result_array() as $row)
{
$pass_auth = $row['member_pass'];
if($password == $pass_auth)
{
$row_data = array(
'member_id'   => $row['member_id'],
'member_user' => $row['member_user']
);
$this->session->set_userdata($row_data);
redirect('member/C_Member');
}
else
{
//if wrong password
$this->load->view('V_Login');
}
}
}
else
{
//if wrong username
$this->load->view('V_Login');
}
}
    public function logout(){
        $this->session->unset_userdata('admin_id');
        $this->session->unset_userdata('admin_user');
        $this->session->unset_userdata('member_id');
        $this->session->unset_userdata('member_user');
        redirect(site_url(''));
    }
}

Buat file Model Utama - M_Main

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_Main extends CI_Model
{
public function get_admin($username,$password)
{
$query = $this->db->query("SELECT * FROM admin WHERE admin_user='$username' AND admin_pass='$password' ");
return $query;
}
public function get_member($username,$password)
{
$query = $this->db->query("SELECT * FROM member WHERE member_user='$username' AND member_pass='$password' ");
return $query;
}
}

Untuk melakukan cek session pada setiap controller, kita membutuhkan sebuah file helper. Maka buatlah file pada folder application/helpers


sessions_helper.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
function admin_logged_in()
{
    $CI =& get_instance();
    $is_logged_in = $CI->session->userdata('admin_user');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        redirect('C_Main');
    }    
}
function member_logged_in()
{
    $CI =& get_instance();
    $is_logged_in = $CI->session->userdata('member_user');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        redirect('C_Main');
    }
}

Lalu buka file autoload.php pada folder application/config dan ubah bagian helper menjadi seperti ini


$autoload['helper'] = array('url','form','sessions');


Nanti di setiap Controller baik Admin maupun Member tambahkan helper session tadi untuk melakukan pengecekan user yang login

Controller Admin - C_Admin

class C_Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
admin_logged_in();
}


Controller Member - C_Member

class C_Member extends CI_Controller
{
public function __construct()
{
parent::__construct();
member_logged_in();
}


Selesai.. bagaimana mudah bukan?

Untuk source codenya silahkan buka Github saya klik disini

Selamat mencoba


Comments

Popular posts from this blog

Cara Membuat Halaman Login Hotspot Berbeda pada 1 Mikrotik

UltraISO Premium Edition v9.5.3

Arti OSAKMJ