Assalamu'alaikum, hallo semuanya! Kembali lagi dengan saya Fadli Mahesa, seorang siswa yang bersekolah di SMKN 4 Bandung dan duduk di bangku kelas XI RPL 3. Kali ini saya akan membagikan lagi sedikit ilmu di dunia percodingan, yaitu tentang Contoh Login & Authorization pada PHP. Mari kita bahas!
Karena materi kali ini merupakan lanjutan dari postingan sebelumnya, maka sebelum kita memasuki codingannya kita akan membuat tabel pada database yang sebelumnya sudah digunakan. Contoh tabelnya seperti ini :
Praktikum16/assets
Syntax ale.css :
body{
margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px
}
h1,h2,h3{
line-height:1.2
}
a{
text-decoration: none;
}
.centertext{
text-align: center;
}
Praktikum16/lib
Syntax helper.php :
<?php
session_start();
function base_url(){
return "http://localhost/Praktikum18";
}
function flash($tipe, $pesan = ''){
if (empty($pesan)) {
$pesan = @$_SESSION[$tipe];
unset($_SESSION[$tipe]);
return $pesan;
} else {
$_SESSION[$tipe] = $pesan;
}
}
function cekLogin() {
$username = @$_SESSION['username'];
$level = @$_SESSION['level'];
if (empty($username) AND empty($level)) {
header("location:login.php");
}
}
function sudahLogin() {
$username = @$_SESSION['username'];
$level = @$_SESSION['level'];
if (!empty($username) AND !empty($level)) {
header("location:index.php");
}
}
?>
Syntax library.php :
<?php
include'lib/helper.php';
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_pwpb21';
$mysqli = mysqli_connect($host, $user, $pass, $db) or die('Tidak dapat Koneksi ke database');
?>
Praktikum16/views
Syntax v_index.php :
<!DOCTYPE html>
<html>
<head>
<title>Praktikum 16, By Fadli Mahesa XI RPL 3</title>
<link rel="stylesheet" type="text/css" href="assets/ale.css">
</head>
<body>
<h1 class="centertext">Database Siswa</h1>
<h2>Data yang ada</h2>
<table border="1">
<thread>
<tr>
<th scope="col">#</th>
<th scope="col">Nis</th>
<th scope="col">Nama Lengkap</th>
<th scope="col">Gender</th>
<th scope="col">Kelas</th>
<th scope="col">Jurusan</th>
<th scope="col">Alamat</th>
<th scope="col">Gol Darah</th>
<th scope="col">Nama Orang Tua</th>
</tr>
</thread>
<tbody>
<?php
$i = 1;
while ($siswa = $listSiswa->fetch_array()) {
?>
<tr>
<th scope="row"><?= $i++; ?></th>
<td><?= $siswa['nis']; ?></td>
<td><?= $siswa['nama_lengkap']; ?></td>
<td><?= $siswa['jenis_kelamin']; ?></td>
<td><?= $siswa['kelas']; ?></td>
<td><?= $siswa['jurusan']; ?></td>
<td><?= $siswa['alamat']; ?></td>
<td><?= $siswa['gol_darah']; ?></td>
<td><?= $siswa['nama_ortu']; ?></td>
<td>
<a href="edit.php?nis=<?= $siswa["nis"]; ?>" class="badge badge-primary">Edit</a>
<a href="delete.php?nis=<?= $siswa["nis"]; ?>" class="badge badge-danger" onclick="return confirm('Apakah anda yakin ingin menghapus data tersebut ?')">Delete</a></td>
</tr>
<?php } ?>
</tbody>
</table>
<br>
<h2>Aksi yang bisa dilakukan</h2>
<a href="tambah.php">Tambah Data</a>
<a href="logout.php">Logout</a>
</body>
</html>
Syntax v_login.php :
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>PWPB Praktikum 16, By Fadli Mahesa</title>
<style type="text/css">
body{
margin:40px auto;
max-width:650px;
line-height:1.6;
font-size:18px;
color:#444;
padding:0
10px}
h1,h2,h3{
line-height:1.2}
</style>
</head>
<body>
<h1>
Login
</h1>
<form class="form-horizontal" action="login.php" method="POST">
<div class="form-group">
<label>Username</label>
<div class="">
<input type="text" name="username" value="">
</div>
</div>
<div class="from-group">
<label>Password</label>
<div class="">
<input type="password" name="password" value="">
</div>
</div>
<div class="form-group">
<label></label>
<div class="">
<button type="submit">Login</button>
</div>
</div>
</form>
</body>
</html>
Syntax v_tambah.php :
<!DOCTYPE html>
<html>
<head>
<title>Praktikum 16, By Fadli Mahesa</title>
<link rel="stylesheet" type="text/css" href="assets/ale.css">
</head>
<body>
<?php
$action = 'tambah.php';
if (!empty($siswa)) $action = 'edit.php'
?>
<form method="POST" action="<?= $action ?>">
<div>
<div >
<span>NIS</span>
</div>
<input type="text" placeholder="Masukkan Nomor induk siswa" name="nis" value="<?= @$siswa['nis'] ?>" required>
</div>
<div>
<div>
<span>Nama Lengkap</span>
</div>
<input type="text" placeholder="Masukkan Nama Lengkap" name="nama_lengkap" value="<?= @$siswa['nama_lengkap'] ?>" required>
</div>
<div>
<label>
<h5>Jenis Kelamin :</h5>
<input type="radio" name="jenis_kelamin" value="L" <?= @$siswa['jenis_kelamin'] == 'L' ? 'checked' : '' ?> required> Laki-laki
</label>
<label>
<input type="radio" name="jenis_kelamin" value="P" <?= @$siswa['jenis_kelamin'] == 'P' ? 'checked' : '' ?>> Perempuan
</label>
</div>
<div>
<div>
<label>Kelas</label>
</div>
<select class="custom-select" id="inputGroupSelect01" name="kelas" required>
<option value="XI MM" <?= @$siswa['kelas'] == 'XI MM' ? 'selected' : '' ?>>XI MM</option>
<option value="XI RPL 1" <?= @$siswa['kelas'] == 'XI RPL 1' ? 'selected' : '' ?>>XI RPL 1</option>
<option value="XI RPL 2" <?= @$siswa['kelas'] == 'XI RPL 2' ? 'selected' : '' ?>>XI RPL 2</option>
<option value="XI RPL 3" <?= @$siswa['kelas'] == 'XI RPL 3' ? 'selected' : '' ?>>XI RPL 3</option>
<option value="XI TKJ 1" <?= @$siswa['kelas'] == 'XI TKJ 1' ? 'selected' : '' ?>>XI TKJ 1</option>
<option value="XI TKJ 2" <?= @$siswa['kelas'] == 'XI TKJ 2' ? 'selected' : '' ?>>XI TKJ 2</option>
<option value="XI TOI 1" <?= @$siswa['kelas'] == 'XI TOI 1' ? 'selected' : '' ?>>XI TOI 1</option>
<option value="XI TOI 2" <?= @$siswa['kelas'] == 'XI TOI 2' ? 'selected' : '' ?>>XI TOI 2</option>
<option value="XI TITL 1" <?= @$siswa['kelas'] == 'XI TITL 1' ? 'selected' : '' ?>>XI TITL 1</option>
<option value="XI TITL 2" <?= @$siswa['kelas'] == 'XI TITL 2' ? 'selected' : '' ?>>XI TITL 2</option>
<option value="XI AVI 1" <?= @$siswa['kelas'] == 'XI AVI 1' ? 'selected' : '' ?>>XI AVI 1</option>
<option value="XI AVI 2" <?= @$siswa['kelas'] == 'XI AVI 2' ? 'selected' : '' ?>>XI AVI 2</option>
<option value="XI AVI 3" <?= @$siswa['kelas'] == 'XI AVI 3' ? 'selected' : '' ?>>XI AVI 3</option>
<option value="XI AVI 4" <?= @$siswa['kelas'] == 'XI AVI 4' ? 'selected' : '' ?>>XI AVI 4</option>
</select>
</div>
<div>
<div>
<span>Jurusan</span>
</div>
<input type="text" placeholder="Masukkan Jurusan anda" name="jurusan" value="<?= @$siswa['jurusan']; ?>" required>
</div>
<div>
<div>
<span>Alamat</span>
</div>
<input type="text" placeholder="Masukkan Alamat Rumah" name="alamat" value="<?= @$siswa['alamat'] ?>" required>
</div>
<div>
<div>
<label>Golongan Darah</label>
</div>
<select name="gol_darah" required>
<option value="A" <?= @$siswa['gol_darah'] == 'A' ? 'selected' : '' ?>>A</option>
<option value="B" <?= @$siswa['gol_darah'] == 'B' ? 'selected' : '' ?>>B</option>
<option value="AB" <?= @$siswa['gol_darah'] == 'AB' ? 'selected' : '' ?>>AB</option>
<option value="O" <?= @$siswa['gol_darah'] == 'O' ? 'selected' : '' ?>>O</option>
</select>
</div>
<div>
<div>
<span>Nama Orang Tua</span>
</div>
<input type="text" placeholder="Masukkan Nama Orang Tua Anda" name="nama_ortu" value="<?= @$siswa['nama_ortu']; ?>" required>
</div>
<button type="submit" value="Simpan">Simpan</button>
</form>
<a href="index.php">Kembali ke data</a>
<script>
const del = document.querySelector(".del");
const overwrite = document.createElement("input");
const inp = document.querySelector(".inp");
if (del.value != '') {
del.setAttribute("type", "hidden");
overwrite.setAttribute("type", "text");
overwrite.setAttribute("class", "form-control");
overwrite.setAttribute("value", "<?= $siswa["nis"]; ?>");
overwrite.setAttribute("disabled", "");
inp.appendChild(overwrite);
}
</script>
</body>
</html>
Praktikum16/
Syntax delete.php :
<?php
include 'lib/library.php';
$nis = $_GET['nis'];
$sql = "DELETE FROM siswa WHERE nis = $nis";
$mysqli->query($sql) or die($mysqli->error);
header('location: index.php'); ?>
Syntax edit.php :
<?php
include 'lib/library.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nis = $_POST['nis'];
$nama_lengkap = $_POST['nama_lengkap'];
$jenis_kelamin = $_POST['jenis_kelamin'];
$kelas = $_POST['kelas'];
$jurusan = $_POST['jurusan'];
$alamat = $_POST['alamat'];
$gol_darah = $_POST['gol_darah'];
$nama_ortu = $_POST['nama_ortu'];
$sql = "UPDATE siswa SET
nama_lengkap = '$nama_lengkap',
jenis_kelamin = '$jenis_kelamin',
kelas = '$kelas',
jurusan = '$jurusan',
alamat = '$alamat',
gol_darah = '$gol_darah',
nama_ortu = '$nama_ortu'
WHERE nis = $nis;
";
$mysqli->query($sql) or die($mysqli->error);
header('location: index.php');
}
$nis = $_GET['nis'];
if (empty($nis)) header('location: index.php');
$sql = "SELECT * FROM siswa WHERE nis = '$nis'";
$query = $mysqli->query($sql);
$siswa = $query->fetch_array();
if (empty($siswa)) header('location: index.php');
include 'views/v_tambah.php';
?>
Syntax index.php :
<?php
include 'lib/library.php';
cekLogin();
$sql = 'SELECT * FROM siswa';
$listSiswa = $mysqli ->query($sql);
include 'views/v_index.php';
?>
Syntax login.php :
<?php
include 'lib/library.php';
sudahLogin();
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM t_login
WHERE username = '$username'
AND password = SHA1('$password')";
$data = $mysqli->query($sql) or die($mysqli->error);
if ($data->num_rows != 0) {
$row = mysqli_fetch_object($data);
$_SESSION['username'] = $row->username;
$_SESSION['level'] = $row->level;
header('location:index.php');
} else {
$error = "Username atau password salah";
}
}
include 'views/v_login.php';
?>
Syntax logout.php :
<?php
session_start();
session_destroy();
header('location:index.php')
?>
Syntax tambah.php :
<?php
include 'lib/library.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nis = $_POST['nis'];
$nama_lengkap = $_POST['nama_lengkap'];
$jenis_kelamin = $_POST['jenis_kelamin'];
$kelas = $_POST['kelas'];
$jurusan = $_POST['jurusan'];
$alamat = $_POST['alamat'];
$gol_darah = $_POST['gol_darah'];
$nama_ortu = $_POST['nama_ortu'];
$sql = "INSERT INTO siswa (nis,nama_lengkap,jenis_kelamin,kelas,jurusan,alamat,gol_darah,nama_ortu)
VALUES ('$nis','$nama_lengkap','$jenis_kelamin','$kelas','$jurusan','$alamat','$gol_darah','$nama_ortu')";
$mysqli->query($sql) or die($mysqli->error);
header("location:index.php");
}
include 'views/v_tambah.php';
?>
Hasil :