![]() |
Ihza Rizky Blog |
Bismillah, Pada Artikel kali ini saya akan membahas mengenai cara "Membuat MultiLevel Login CodeIgniter". Dalam sebuah website modern dan kompleks pasti ada sebuah sistem yang memisahkan antara user dengan admin untuk membedakan peran posisi tersebut. Misalnya, user bisa melakukan aktivitas posting artikel, komentar dan edit postingan serta komentar. Lalu, Admin bisa menghapus sebuah postingan dan menghapus user dari database.
Akan tetapi, Artikel kali ini akan membahas mengenai sistem login dengan level berbeda antara user dan admin. Jika user login, akan diarahkan ke halaman user dan apabila yang login adalah admin akan diarahkan ke halaman admin.
Hal - hal yang harus disiapkan sebagai berikut :
- CodeIgniter 3
- Xampp / Lampp (Linux)
- Text Editor
- Browser (Rekomendasi Google Chrome)
- Internet
Nah, saya asumsikan anda sudah tau bagaimana cara install CodeIgniter 3 secara manual https://codeigniter.com/download
- Lalu, selanjutnya setting Autoload dalam folder application yaitu $autoload['libraries'] = array('database','session');
- Setting juga helpernya didalam Autoload $autoload['helper'] = array('url');
- Buat Database dengan nama login misalnya dan tabel user dengan kolom id, username, password dan level
- lalu, setting nama database, localhost, username, password didalam folder application - config dan file database.php
- Buat model dengan nama LoginModel.php dan isikan script berikut :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class LoginModel extends CI_Model{ | |
public function checkUser($username,$password) | |
{ | |
$this->db->where("username",$username); | |
$this->db->where("password",$password); | |
$query = $this->db->get("user"); | |
return $query; | |
} | |
} |
- Buat Controller Home.php dan Page.php :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
// Author : Mochammad Ihza Rizky Karim | |
class Home extends CI_Controller{ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->model("LoginModel"); | |
} | |
public function index() | |
{ | |
$data['judul'] = "<h1>Selamat Datang</h1>"; | |
$this->load->view("login",$data); | |
} | |
public function auth() | |
{ | |
$username = $this->input->post("username",true); | |
$password = $this->input->post("password",true); | |
$check = $this->LoginModel->checkUser($username,$password); | |
if($check->num_rows() > 0) | |
{ | |
$data = $check->row_array(); | |
$username = $data['username']; | |
$password = $data['password']; | |
$level = $data['level']; | |
$data_session = array("nama" => $username, "password" => $password, "level" => $level, "logged" => TRUE); | |
$this->session->set_userdata($data_session); | |
if($level == "admin") | |
{ | |
redirect("Page/admin"); | |
} | |
elseif($level == "user") | |
{ | |
redirect("Page/user"); | |
}else{ | |
redirect("Home"); | |
} | |
} | |
else{ | |
echo $this->session->set_flashdata("failed","Username dan Password Salah"); | |
echo $this->session->flashdata("failed"); | |
} | |
} | |
public function logout() | |
{ | |
$this->session->sess_destroy(); | |
redirect("Home"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
// Author : Mochammad Ihza Rizky Karim | |
class Page extends CI_Controller{ | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->load->model("LoginModel"); | |
if($this->session->userdata("logged") == FALSE) | |
{ | |
redirect("Home"); | |
} | |
} | |
public function user() | |
{ | |
if($this->session->userdata("level") == "user") | |
{ | |
$this->load->view("page/user"); | |
}else{ | |
echo "Akses Ditolak"; | |
} | |
} | |
public function admin() | |
{ | |
if($this->session->userdata("level") == "admin") | |
{ | |
$this->load->view("page/admin"); | |
}else{ | |
echo "Akses Ditolak"; | |
} | |
} | |
} |
- Lalu buat folder page yang didalam ada 2 file view yaitu user.php dan admin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>User Page</title> | |
</head> | |
<body> | |
<h1> | |
Selamat Datang | |
<?php if($this->session->userdata("level") == "user"){ | |
echo "Role ".$this->session->userdata("level"); | |
} ?> | |
</h1> | |
<h1>INI HALAMAN USER</h1> | |
<p> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</p> | |
<br> | |
<a href="<?= base_url().'Home/logout'; ?>"> | |
Keluar | |
</a> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Admin Page</title> | |
</head> | |
<body> | |
<h1> | |
Selamat Datang | |
<?php if($this->session->userdata("level") == "admin"){ | |
echo "Role ".$this->session->userdata("level"); | |
} ?> | |
</h1> | |
<h1>INI HALAMAN ADMIN</h1> | |
<p> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, | |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo | |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse | |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</p> | |
<br> | |
<a href="<?= base_url().'Home/logout'; ?>"> | |
Keluar | |
</a> | |
</body> | |
</html> |
- Terakhir buat file login.php didalam folder view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Login Page</title> | |
</head> | |
<body> | |
<?= $judul; ?> | |
<br> | |
<form action="<?= base_url().'Home/auth';?>" method="POST"> | |
<label> | |
Username : | |
</label> | |
<br> | |
<input type="text" name="username"> | |
<br><br> | |
<label> | |
Password : | |
</label> | |
<br> | |
<input type="password" name="password"> | |
<br><br> | |
<button type="submit" name="login"> | |
Login | |
</button> | |
</form> | |
</body> | |
</html> |
dan sekarang coba jalankan server xampp / lampp untuk mencoba skripnya
untuk admin usernamenya adalah admin dan passwordnya juga admin, lalu untuk user usernamenya adalah user dan passwordnya adalah user dengan level admin.
mantap
BalasHapus