Tugas 1
Tugas 1
Nim: 1904411358
Tugas 1
Source Code
1.index.php
<?php
session_start();
<!DOCTYPE html>
<html>
<head>
<title>table_user</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1"
crossorigin="anonymous">
</head>
<body>
style="min-height: 100vh">
action="php/check-login.php"
method="post"
style="width: 450px;">
<?=$_GET['error']?>
</div>
<?php } ?>
<div class="mb-3">
<label for="username"
class="form-label">User name</label>
<input type="text"
class="form-control"
name="username"
id="username">
</div>
<div class="mb-3">
<label for="password"
class="form-label">Password</label>
<input type="password"
name="password"
class="form-control"
id="password">
</div>
<div class="mb-1">
</div>
name="role"
<option value="admin">Admin</option>
</select>
<button type="submit"
class="btn btn-primary">LOGIN</button>
</form>
</div>
</body>
</html>
<?php }else{
header("Location: home.php");
} ?>
2.home.php
<?php
session_start();
include "db_conn.php";
<!DOCTYPE html>
<html>
<head>
<title>HOME</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1"
crossorigin="anonymous">
</head>
<body>
style="min-height: 100vh">
class="card-img-top"
alt="admin image">
<h5 class="card-title">
<?=$_SESSION['name']?>
</h5>
</div>
</div>
<div class="p-3">
<table class="table"
style="width: 32rem;">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Role</th>
</tr>
</thead>
<tbody>
<?php
$i =1;
<th scope="row"><?=$i?></th>
<td><?=$rows['name']?></td>
<td><?=$rows['username']?></td>
<td><?=$rows['role']?></td>
</tr>
</tbody>
</table>
<?php }?>
</div>
<img src="img/user-default.png"
class="card-img-top"
alt="admin image">
<h5 class="card-title">
<?=$_SESSION['name']?>
</h5>
</div>
</div>
<?php } ?>
</div>
</body>
</html>
<?php }else{
header("Location: index.php");
} ?>
3.check-login.php
<?php
session_start();
include "../db_conn.php";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
$username = test_input($_POST['username']);
$password = test_input($_POST['password']);
$role = test_input($_POST['role']);
if (empty($username)) {
}else if (empty($password)) {
}else {
if (mysqli_num_rows($result) === 1) {
$row = mysqli_fetch_assoc($result);
$_SESSION['name'] = $row['name'];
$_SESSION['id'] = $row['id'];
$_SESSION['role'] = $row['role'];
$_SESSION['username'] = $row['username'];
header("Location: ../home.php");
}else {
}else {
}else {
header("Location: ../index.php");
}
4.logout.php
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");
5.members.php
<?php
}else{
header("Location: index.php");
6.db_coon.php
<?php
$sname = "localhost";
$uname = "root";
$password = "";
$db_name = "my_db";
$conn = mysqli_connect($sname, $uname, $password, $db_name);
if (!$conn) {
exit();
1.login admin
2.halaman admin
3.login user/members
4.halaman user/members