35370-vm/login.php
Flatlogic Bot 275165284b 1.0
2025-10-31 00:13:36 +00:00

88 lines
2.8 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/database.php';
$error_message = '';
// If already logged in, redirect to main page
if (isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if (empty($username) || empty($password)) {
$error_message = 'Please enter both username and password.';
} else {
$pdo = get_db_connection();
$stmt = $pdo->prepare("SELECT id, password FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $username;
header('Location: index.php');
exit;
} else {
$error_message = 'Invalid username or password.';
}
}
}
$page_title = "Login";
// We don't include the main header as it would create a redirect loop.
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($page_title) ?> - Member Management</title>
<link rel="stylesheet" href="assets/css/style.css?v=<?php echo time(); ?>">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f8f9fa;
}
.login-container {
background: #fff;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
width: 100%;
max-width: 400px;
}
</style>
</head>
<body>
<div class="login-container">
<h1>Login</h1>
<p>Welcome to the Member Management System.</p>
<?php if ($error_message): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error_message) ?></div>
<?php endif; ?>
<form action="login.php" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" required autofocus>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<div class="alert alert-info" style="margin-top: 1rem;">
Use <strong>admin</strong> / <strong>password</strong> to login.
</div>
</div>
</body>
</html>