Multi-level User Login with PHP & MySQLi


Disini dimisalkan sebuah sistem yang memiliki 3 level user berbeda.

Contoh:
 - Admin
 - Dosen
 - Mahasiswa


1) Pertama buat halaman loginnya

<h1> FORM LOGIN </h1>
<form action="proses_login.php" method="post">
<input type="text" name="username" required>
<input type="password" name="password" required>
<button type="submit">LOGIN</button>
</form>

2) Lalu buat file actionnya (proses_login.php)
<?php
@session_start();
include 'koneksi.php'; //ganti dengan koneksi database anda
$username = $_POST['username'];
$password = $_POST['password'];
if($username&&$password){
//cek data admin
$get_admin = mysqli_query($conn,"SELECT * FROM tb_admin WHERE user_admin='$username'");
$cek_admin = mysqli_num_rows($get_admin);
if($cek_admin!=0){
while($row = mysqli_fetch_assoc($get_admin)){
$dbusername = $row['user_admin'];
$dbpassword = $row['pass_admin'];
}
if($username==$dbusername&&$password==$dbpassword){
$_SESSION['user_admin']=$username;
header("location:./admin/dashboard");
}
else{
header("location:/index.php");
}
}
//jika data admin tidak ada, cek data dosen
else{
$get_dosen = mysqli_query($conn,"SELECT * FROM tb_dosen WHERE user_dosen='$username'");
$cek_dosen = mysqli_num_rows($get_dosen);
if($cek_dosen!=0){
while($row = mysqli_fetch_assoc($get_dosen)){
$dbusername = $row['user_dosen'];
$dbpassword = $row['pass_dosen'];
}
if($username==$dbusername&&$password==$dbpassword){
$_SESSION['user_dosen']=$username;
header("location:./dosen/dashboard");
}
else{
header("location:/index.php");
}
}
//jika data dosen tidak ada, cek data mahasiswa
else{
$get_mhs = mysqli_query($conn,"SELECT * FROM tb_mhs WHERE user_mhs='$username'");
$cek_mhs = mysqli_num_rows($get_mhs);
if($cek_mhs!=0){
while($row = mysqli_fetch_assoc($get_mhs)){
$dbusername = $row['user_mhs'];
$dbpassword = $row['pass_mhs'];
}
if($username==$dbusername&&$password==$dbpassword){
$_SESSION['user_mhs']=$username;
header("location:./mahasiswa/dashboard");
}
else{
header("location:/index.php");
}
}
}
}
}
else{
header("location:/index.php");
}
?>

3) File proses_login.php tersebut melakukan cek satu-persatu menggunakan IF-ELSE sederhana
kamu bisa menambahkan lagi sesuai jumlah kebutuhan hak akses dengan cara yang sama

4) Selanjutnya kode tersebut tinggal di terapkan pada sistem yang kamu buat

Selamat mencoba!

0 comments:

Post a Comment

What do you want? Just leave a comment :)