Membuat Login Multiuser & Multilevel dengan PHP & MySQL

Halo semua, kali ini kita akan membahas mengenai login multiuser dan multilevel dengan PHP dan MySQL, oke langsung saja.... :D

pertama kita buat database nya dengan struktur berikut

mysql> desc pengguna;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(10)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | NO   |     | NULL    |                |
| password | varchar(32)  | NO   |     | NULL    |                |
| akses    | varchar(32)  | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0,01 sec)

serta kita isikan dengan beberapa data

mysql> select*from pengguna;
+----+------------+----------------------------------+----------+
| id | username   | password                         | akses    |
+----+------------+----------------------------------+----------+
|  1 | ariefsetya | fbf729726af3571adfb3067cf76803f6 | admin    |
|  2 | opera      | 9ed987966c06808b50f9fddc24931bd1 | operator |
|  3 | user       | ee11cbb19052e40b07aac0ca060c23ee | user     |
+----+------------+----------------------------------+----------+
3 rows in set (0,00 sec)

kemudian kita buat struktur file sebagai berikut

selanjutnya kita buka file nya satu per satu... ^_^

admin.php

<?php 
if($_SESSION['level']=="admin"){
?>
Halo, <?php echo $_SESSION['username'];?><br>
Anda masuk sebagai <?php echo $_SESSION['level'];?>
<?php
}else{
	echo "Anda tidak mempunyai hak untuk mengakses halaman ini";
}
?>

ceklogin.php

<?php
	include "koneksi.php";

	if(isset($_POST['username'])){
		$username = $_POST['username'];
		$password = md5($_POST['password']);

		$a = mysql_query("select*from pengguna where username='".$username."' and password = '".$password."'");
		if(mysql_num_rows($a)>0){
			$row = mysql_fetch_row($a);
			$_SESSION['level'] = $row[3];
			$_SESSION['username'] = $row[1];
			$_SESSION['idlogin'] = $row[0];
			$_SESSION['islogin'] = true;
			header("location:index.php?r=home");
		}else{
			echo "Maaf, informasi login yang Anda masukkan tidak terdaftar... <a href='index.php?r=login'>Kembali ke halaman login</a>";
		}
	}
?>

css.css

.divmenu div{
	display:inline;
}

home.php

<?php
if($_SESSION['islogin']==false){
	echo "Welcome, guest....";
}else{
	if($_SESSION['level']=="admin"){
		echo "Welcome home, Administrator....";
	}
	if($_SESSION['level']=="operator"){
		echo "Welcome back, Operator....";
	}
	if($_SESSION['level']=="user"){
		echo "Welcome, User....";
	}
}
?>

index.php

<?php
include 'koneksi.php';
?>
<!DOCTYPE html>
<html>
<head>
	<title>Login Multiuser</title>
	<link rel="stylesheet" type="text/css" href="css.css">
</head>
<body>
<header><?php include "menu.php";?></header>
	<?php
	if(!isset($_GET['r'])){
		include "home.php";
	}else{
		$r = $_GET['r'];
		switch ($r) {
			case 'home':
				include "home.php";
				break;
			case 'login':
				include "login.php";
				break;
			case 'logout':
				include "logout.php";
				break;
			case 'guest':
				include "home.php";
				break;
			case 'menuuser':
				include "user.php";
				break;
			case 'menuadmin':
				include "admin.php";
				break;
			case 'menuoperator':
				include "operator.php";
				break;
			
			default:
				include "home.php";
				break;
		}
	}
	?>
</body>
</html>

koneksi.php

<?php
	session_start();
	@mysql_connect("localhost","root","windows10") or die("Server mati");
	@mysql_select_db("syslogin") or die("Database hilang");
?>

login.php

<form method="POST" action="ceklogin.php">
	<table>
		<tr>
			<td>Username</td>
			<td><input type="text" required name="username"></td>
		</tr>
		<tr>
			<td>Password</td>
			<td><input type="password" required name="password"></td>
		</tr>
		<tr>
			<td></td>
			<td><button type="submit">Masuk</button></td>
		</tr>
	</table>
</form>

logout.php

<?php
$_SESSION['level'] = "";
$_SESSION['islogin'] = false;
$_SESSION['username'] = "";
$_SESSION['idlogin'] = "";
?>
Anda telah logout... <a href="index.php?r=home">Kembali ke home</a>

menu.php

<div class="divmenu">
	<?php
	if($_GET['r']!="logout"){
		if($_SESSION['level']=="admin"){
		?>
		<div><a href="?r=menuadmin">Menu Administrator</a></div>
		<div><a href="?r=logout">Logout</a></div>
		<?php
		}
		elseif($_SESSION['level']=="operator"){
		?>
		<div><a href="?r=menuoperator">Menu Operator</a></div>
		<div><a href="?r=logout">Logout</a></div>
		<?php
		}
		elseif($_SESSION['level']=="user"){
		?>
		<div><a href="?r=menuuser">Menu User</a></div>
		<div><a href="?r=logout">Logout</a></div>
		<?php
		}
		elseif($_SESSION['level']==""){
		?>
		<div><a href="?r=guest">Menu Guest</a></div>
		<div><a href="?r=login">Login</a></div>
		<?php
		}
	}
	?>
</div>

operator.php

<?php 
if($_SESSION['level']=="operator"){
?>
Halo, <?php echo $_SESSION['username'];?><br>
Anda masuk sebagai <?php echo $_SESSION['level'];?>
<?php
}else{
	echo "Anda tidak mempunyai hak untuk mengakses halaman ini";
}
?>

user.php

<?php 
if($_SESSION['level']=="user"){
?>
Halo, <?php echo $_SESSION['username'];?><br>
Anda masuk sebagai <?php echo $_SESSION['level'];?>
<?php
}else{
	echo "Anda tidak mempunyai hak untuk mengakses halaman ini";
}
?>

Source code nya bisa di unduh disini

berikut penampakannya.... wink




Thursday, 17 Sep 2015 12:28 PM Arief Setya
Sign In
Sign In to Your Account :)
Sign In or Sign Up
 
Follow Us on Twitter
Find Us on Facebook