0% found this document useful (0 votes)
60 views10 pages

Tugas 1

Uploaded by

t56kmnj8f2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views10 pages

Tugas 1

Uploaded by

t56kmnj8f2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Nama: Firgi Artia

Nim: 1904411358

Kelas: 6 gab 1 web

Matkul: Website Berbasis Framework

Tugas 1

Source Code

1.index.php

<?php

session_start();

if (!isset($_SESSION['username']) && !isset($_SESSION['id'])) { ?>

<!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>

<div class="container d-flex justify-content-center align-items-center"

style="min-height: 100vh">

<form class="border shadow p-3 rounded"

action="php/check-login.php"

method="post"

style="width: 450px;">

<h1 class="text-center p-3">LOGIN</h1>

<?php if (isset($_GET['error'])) { ?>

<div class="alert alert-danger" role="alert">

<?=$_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">

<label class="form-label">Select User Type:</label>

</div>

<select class="form-select mb-3"

name="role"

aria-label="Default select example">

<option selected value="user">User</option>

<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";

if (isset($_SESSION['username']) && isset($_SESSION['id'])) { ?>

<!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>

<div class="container d-flex justify-content-center align-items-center"

style="min-height: 100vh">

<?php if ($_SESSION['role'] == 'admin') {?>

<!-- For Admin -->

<div class="card" style="width: 18rem;">


<img src="img/admin-default.png"

class="card-img-top"

alt="admin image">

<div class="card-body text-center">

<h5 class="card-title">

<?=$_SESSION['name']?>

</h5>

<a href="logout.php" class="btn btn-dark">Logout</a>

</div>

</div>

<div class="p-3">

<?php include 'php/members.php';

if (mysqli_num_rows($res) > 0) {?>

<h1 class="display-4 fs-1">Members</h1>

<table class="table"

style="width: 32rem;">

<thead>

<tr>

<th scope="col">#</th>

<th scope="col">Name</th>

<th scope="col">User name</th>

<th scope="col">Role</th>

</tr>

</thead>

<tbody>

<?php

$i =1;

while ($rows = mysqli_fetch_assoc($res)) {?>


<tr>

<th scope="row"><?=$i?></th>

<td><?=$rows['name']?></td>

<td><?=$rows['username']?></td>

<td><?=$rows['role']?></td>

</tr>

<?php $i++; }?>

</tbody>

</table>

<?php }?>

</div>

<?php }else { ?>

<!-- FORE USERS -->

<div class="card" style="width: 18rem;">

<img src="img/user-default.png"

class="card-img-top"

alt="admin image">

<div class="card-body text-center">

<h5 class="card-title">

<?=$_SESSION['name']?>

</h5>

<a href="logout.php" class="btn btn-dark">Logout</a>

</div>

</div>

<?php } ?>

</div>

</body>

</html>

<?php }else{
header("Location: index.php");

} ?>

3.check-login.php

<?php

session_start();

include "../db_conn.php";

if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['role'])) {

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)) {

header("Location: ../index.php?error=User Name is Required");

}else if (empty($password)) {

header("Location: ../index.php?error=Password is Required");

}else {

// Hashing the password


$password = md5($password);

$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) === 1) {

// the user name must be unique

$row = mysqli_fetch_assoc($result);

if ($row['password'] === $password && $row['role'] == $role) {

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

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

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

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

header("Location: ../home.php");

}else {

header("Location: ../index.php?error=Incorect User name or password");

}else {

header("Location: ../index.php?error=Incorect User name or password");

}else {

header("Location: ../index.php");

}
4.logout.php

<?php

session_start();

session_unset();

session_destroy();

header("Location: index.php");

5.members.php

<?php

if (isset($_SESSION['username']) && isset($_SESSION['id'])) {

$sql = "SELECT * FROM users ORDER BY id ASC";

$res = mysqli_query($conn, $sql);

}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) {

echo "Connection Failed!";

exit();

Tampilan Layar Hasil Login

1.login admin

2.halaman admin
3.login user/members

4.halaman user/members

You might also like