WEB_PROJECT_TA/login.php

130 lines
3.6 KiB
PHP

<?php
session_start();
require 'config/database.php'; // memanggil class Database
// Buat koneksi PDO
$db = new Database();
$conn = $db->getConnection();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
try {
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user'] = $user['username'];
$_SESSION['role'] = $user['role'];
header("Location: index.php");
exit;
} else {
$error = "Username atau password salah!";
}
} catch (PDOException $e) {
$error = "Terjadi kesalahan: " . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - FTTH Megadata</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
height: 100vh;
background: url('bg.jpg') no-repeat center center fixed;
background-size: cover;
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
}
.login-container {
background: rgba(255, 255, 255, 0.9);
padding: 40px;
border-radius: 15px;
box-shadow: 0 0 15px rgba(0,0,0,0.2);
text-align: center;
width: 320px;
max-width: 100%;
}
.login-container img {
max-width: 150px;
height: auto;
border-radius: 0;
object-fit: contain;
margin-bottom: 15px;
}
.login-container h2 {
margin-bottom: 20px;
color: #333;
}
.login-container input {
width: 100%;
padding: 12px;
margin: 8px 0;
border: none;
border-radius: 30px;
background: #f1f1f1;
outline: none;
font-size: 14px;
}
.login-container button {
width: 100%;
padding: 12px;
border: none;
border-radius: 30px;
background: #333;
color: #fff;
font-size: 14px;
cursor: pointer;
}
.login-container button:hover {
background: #555;
}
.login-container p {
font-size: 12px;
margin-top: 15px;
}
.login-container a {
color: #0066cc;
text-decoration: none;
}
.error {
color: red;
margin-bottom: 10px;
}
/* Responsif untuk layar kecil */
@media (max-width: 480px) {
.login-container {
padding: 25px;
width: 100%;
}
.login-container img {
max-width: 100px;
}
}
</style>
</head>
<body>
<div class="login-container">
<img src="megadata.jpeg" alt="Logo">
<h2>Login</h2>
<?php if (!empty($error)) echo "<p class='error'>$error</p>"; ?>
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>