34619-vm/admin/login.php
2025-10-03 13:03:26 +00:00

69 lines
2.7 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../includes/security.php';
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!validate_csrf_token()) {
$error_message = 'CSRF token validation failed.';
} elseif (!empty($_POST['username']) && !empty($_POST['password'])) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$_POST['username']]);
$user = $stmt->fetch();
if ($user && password_verify($_POST['password'], $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: index.php");
exit;
} else {
$error_message = "Invalid username or password.";
}
} catch (PDOException $e) {
$error_message = "Database error: " . $e->getMessage();
}
} else {
$error_message = "Please enter both username and password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login - FormFlex Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="../assets/css/admin.css">
</head>
<body>
<div class="login-container">
<div class="login-box">
<h2 class="text-center mb-4">FormFlex Pro</h2>
<h4 class="text-center mb-4">Admin Portal</h4>
<?php if ($error_message): ?>
<div class="alert alert-danger" role="alert">
<?php echo htmlspecialchars($error_message); ?>
</div>
<?php endif; ?>
<form action="login.php" method="POST" autocomplete="off">
<?php echo csrf_input_field(); ?>
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary w-100">Login</button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>