Database
Database: | db_lelang_online |
Table: | tbl_user (id_user, username, password) |
HTML dan PHP: index.php
<?php
// Konfigurasi database
$host = "localhost"; // Host database
$username = "root"; // Username database
$password = ""; // Password database
$database = "db_lelang_online"; // Nama database
// Membuat koneksi ke database
$conn = new mysqli($host, $username, $password, $database);
// Memeriksa koneksi
if ($conn->connect_error) {
die("Koneksi gagal: " . $conn->connect_error);
}
// Memeriksa apakah form login telah dikirim
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
// Mengecek username dan password ke dalam tabel tbl_user
$sql = "SELECT * FROM tbl_user WHERE username = '$username' AND password = '$password'";
$result = $conn->query($sql);
// Jika data ditemukan, maka login berhasil
if ($result->num_rows > 0) {
session_start();
$_SESSION["username"] = $username;
header("location: welcome.php"); // Redirect ke halaman selamat datang
} else {
$error = "Username atau password salah";
}
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h2>Login</h2>
<form action="" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Password" required>
</div>
<button type="submit" class="btn-primary">Login</button>
</form>
<?php
// Menampilkan pesan error jika login gagal
if (isset($error)) {
echo "<p style='color: red;'>$error</p>";
}
?>
</div>
</body>
</html>
CSS: style.css
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
width: 300px;
}
.container h2 {
text-align: center;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.form-group input[type="text"],
.form-group input[type="password"] {
width: 92%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 3px;
outline: none;
}
.form-group input[type="text"]:focus,
.form-group input[type="password"]:focus {
border-color: #007bff;
}
.btn-primary {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 3px;
cursor: pointer;
}
.btn-primary:hover {
background-color: #0056b3;
}
welcome.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Halaman Admin</h1>
</body>
</html>