Latihan Praktikum 5
Kerjakan latihan-latihan modul Praktikum 5 (halaman 12) pada localhost masing-masing. File yang perlu dikumpulkan:
- File welcome.php, login.php, login_action.php
- Screenshoot tampilan browser untuk file login.php
penyelesaian
1. Buatlah database tersetah anda
2. Copy sql berikut, save dengan .sql
3. Buatlah file koneksi.php
<?php
$koneksi = mysqli_connect("localhost", "root", "" ,"data1286");
?>
4. Buatlah form loginya, dan actionya
semisal form login php, dan formact.php. disini saya memakai metod POST, kenapa ? karena kalau GET jadi di url akan ditampilkan parameternya
Source form.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<style>
body{
font-family: sans-serif;
background-color: skyblue;
}
h1{
text-align: center;
/*ketebalan font*/
font-weight: 300;
}
.tulisan_login{
text-align: center;
/*membuat semua huruf menjadi kapital*/
text-transform: uppercase;
}
.kotak_login{
width: 450px;
background: white;
/*meletakkan form ke tengah*/
margin: 70px auto;
padding: 30px 25px;
}
label{
font-size: 11pt;
}
.form_login{
/*membuat lebar form penuh*/
box-sizing : border-box;
width: 100%;
padding: 10px;
font-size: 11pt;
margin-bottom: 20px;
}
.tombol_login{
background: blue;
color: white;
font-size: 11pt;
width: 100%;
border: none;
border-radius: 3px;
padding: 10px 20px;
}
</style>
<meta charset="utf-8">
<title>Login dulu</title>
</head>
<body>
<div class="kotak_login">
<p class="tulisan_login"><h1>Admin amikom news</h1>
<form action="loginact.php" method="post">
Username : <input type="text" class="form_login" name="user" />
<br />
Password : <input type="password" class="form_login" name="password" />
<br />
<input type="submit" class="tombol_login" value="Login" />
</body>
</div>
</html>
Loginact.php
<?php
include 'koneksi.php';
$username = htmlspecialchars($_POST['user']);
$password = htmlspecialchars($_POST['password'])
$login = mysqli_query($koneksi,"SELECT * FROM user WHERE user_nama='$username' AND user_password='$password'");
$cek = mysqli_num_rows($login)
if($cek > 0)
{
session_start();
$data = mysqli_fetch_assoc($login);
$_SESSION['username'] = $username;
$_SESSION['status'] = "login";
header("Location:welcome.php");
exit();
}
else
{
header("Location:login.php");
}
?>
Setelah itu kalian run, hasil output

Welcome.php
<?php
session_start();
if(isset($_SESSION['username'])){ ?>
<h2>Control Panel</h2>
<p>Selamat datang "<?php echo $_SESSION['username']; ?>".
Klik <a href="logut.php">disini</a> untuk logout.</p>
<?php
} else { ?>
<h2>Maaf..</h2>
<p>Anda tidak berhak mengakses halaman ini.
Silakan <a href="login.php">login</a> terlebih dahulu. klik <a href="logut.php">keluar</p>
<?php
}
?>
*Beware click the link!
NONE
Dump sqlnya
sorry lupa
Reply