Tutorial Multi level Login dengan PHP + Mysql

database = login
table = user

Buatlah struktur file seperti di bawah ini


koneksi.php

<?php
$host = "localhost";
$username = "root";
$password ="";
$database = "login";

$connection = mysql_connect($host,$username,$password) or die("no database : ".mysql_error());
$database = mysql_select_db($database);
?>
form.css
.input {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

.button {
    width: 20%;
    background-color: #4CAF50;
    color: white;
    padding: 10px 0px;
    margin: 8px 0;
    border: none;
    border-radius: 1px;
    cursor: pointer;
    font-size: 15px;
}

.button:hover {
    background-color: #45a049;
}

div {
    border-radius: 5px;
    background-color: #f2f2f2;    padding: 20px;
}

.center {
    margin: auto;
    width: 65%;
    border: 10px;
    padding: 40px;
}
/* solid #73AD21; */
.centers {
    margin: auto;
    width: 65%;
}

registrasidosen.php
<html>

<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh registrasi dosen </title>
<body>

<h2 class="centers"> <center> Contoh Registrasi Dosen </center> </h2>
<br>
<div class="center">
<form action="prosesregistrasidosen.php" method="post">
<label>Nama Lengkap </label>
<input type="text" name="nama" required="required" class="input">
<label>NIK </label>
<input type="number" name="nomorinduk" required="required" class="input">
<label>Username </label>
<input type="text" name="username" required="required" class="input">
<label>Password </label>
<input type="password" name="password" required="required" class="input">

<input type="submit" value="Submit" name="submit" class="button">
<input type="reset" value="reset" class="button">
  </form>
</body>
</head>
</html>

prosesregistrasidosen.php
<?php
if(isset($_POST['submit']))
{
include "koneksi.php";

$nama = $_POST['nama'];
$username = $_POST['username'];
$password = $_POST['password'];
$nomorinduk = $_POST['nomorinduk'];

$query = "insert into user (id,nama,nomorinduk,username,password,level)
values ('','$nama','$nomorinduk','$username','$password','Dosen')";
$result = mysql_query($query,$connection) or die("query salah : ".mysql_error());

if($result)
{
echo "registrasi berhasil";
echo "<meta http-equiv='refresh' content='1; url=login.php'>";
}
else
{
echo "registrasi gagal";
?>
<meta http-equiv='refresh' content='1; url=login.php'>;
<?php
}
}

?>

registrasimahasiswa.php
<html>

<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh registrasi mahasiswa </title>
<body>

<h2 class="centers"> <center> Contoh Registrasi Mahasiswa </center> </h2>
<br>
<div class="center">
<form action="prosesregistrasimahasiswa.php" method="post">
<label>Nama Lengkap </label>
<input type="text" name="nama" required="required" class="input">
<label>NIM </label>
<input type="number" name="nomorinduk" required="required" class="input">
<label>Username </label>
<input type="text" name="username" required="required" class="input">
<label>Password </label>
<input type="password" name="password" required="required" class="input">

<input type="submit" value="Submit" name="submit" class="button">
<input type="reset" value="reset" class="button">
  </form>
</body>
</head>
</html>

prosesregistrasimahasiswa.php
<?php
if(isset($_POST['submit']))
{
include "koneksi.php";

$nama = $_POST['nama'];
$username = $_POST['username'];
$password = $_POST['password'];
$nim = $_POST['nomorinduk'];

$query = "insert into user (id,nama,nomorinduk,username,password,level)
values ('','$nama','$nim','$username','$password','Mahasiswa')";
$result = mysql_query($query,$connection) or die("query salah : ".mysql_error());

if($result)
{
echo "registrasi berhasil";
echo "<meta http-equiv='refresh' content='1; url=login.php'>";
}
else
{
echo "registrasi gagal";
?>
<meta http-equiv='refresh' content='1; url=login.php'>;
<?php
}
}

?>

login.php
<html>

<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh login </title>
<body>

<h2 class="centers"> <center> Contoh Login Form </center> </h2>
<br>
<div class="center">
<form action="proseslogin.php" method="post">
<label>Username : </label>
<input type="text" name="username" required="required" class="input">
<label>Password : </label>
<input type="password" name="password" required="required" class="input">
<label> Select Level </label>

<select name="level" class="input">
<option value ="Dosen"> Dosen </option>
<option value ="Mahasiswa"> Mahasiswa </option>
</select>

<input type="submit" value="login" name="submit" class="button">
<input type="reset" value="reset" class="button">
  </form>
</body>
<!--
<script type="text/javascript">
    function bukatab() {
        window.open("registrasi.php");
    }
-->
</script>
</head>
</html>

proseslogin.php
<?php
if(isset($_POST['submit']))
{
session_start();
include "koneksi.php";
$username = $_POST['username'];
$password = $_POST['password'];
$level = $_POST['level'];

if($level == "Mahasiswa")
{
$query = "select * from user where
username='$username' and
password='$password' and
level = '$level'";
$result = mysql_query($query,$connection) or die("Query salah : ".mysql_error());

if(mysql_num_rows($result) >=1)
{
$row = mysql_fetch_array($result);
$_SESSION['username'] = $row['username'];

$_SESSION['level'] = $row['level'];

?>
<meta http-equiv='refresh' content ='0; url=index.php'>
<?php
}
else
{
echo "Username,password atau level yang anda masukkan salah";
?>
<meta http-equiv='refresh' content ='1; url=login.php'>
<?php
}
}
if($level == "Dosen")
{
$query = "select * from user where
username='$username' and
password='$password' and
level = '$level'";
$result = mysql_query($query,$connection) or die("Query salah : ".mysql_error());

if(mysql_num_rows($result) >=1)
{
$row = mysql_fetch_array($result);
$_SESSION['username'] = $row['username'];

$_SESSION['level'] = $row['level'];

?>
<meta http-equiv='refresh' content ='0; url=index.php'>
<?php
}
else
{
echo "Username,password atau level yang anda masukkan salah";
?>
<meta http-equiv='refresh' content ='1; url=login.php'>
<?php
}
}
}


?>

index.php
<?php
session_start();
include "login_session.php";

include "koneksi.php";
?>

<html>
<head>
<title> Login Berhasil </title>
<link rel="stylesheet" type="text/css" href="form.css">
<body>
<br>
<br>
<center>
Selamat Datang
<?php echo $_SESSION['username']; ?>
<br>
Terima kasih sudah login
<br>
anda login sebagai <?php echo $_SESSION['level']; ?>
<br>
<button onclick="location.href='login_out.php';" class="button"> logout </button>
<br>
<?php
if($_SESSION['level'] == "Dosen")
{
?>
<a href ="lihatdata.php"> Lihat Data Mahasiswa </a>
<?php
}
?>
</center>
</body>
</head>
</html>

login_session.php
<?php
if (empty($_SESSION['username']))
{
echo "<meta http-equiv='refresh' content ='0; url=login.php'>";
exit;
}
?>

login.out.php
<?php
session_start();
session_unset();
session_destroy();
echo "<meta http-equiv='refresh' content ='0; url=login.php'>";
exit;
?>







Download File + Database