36163-vm/login.php
2025-11-24 05:17:23 +00:00

81 lines
3.2 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$password = $_POST['password'];
if (empty($username) || empty($password)) {
$error = 'Please fill in all fields.';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: index.php");
exit;
} else {
$error = 'Invalid username or password.';
}
} catch (PDOException $e) {
$error = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html class="dark" lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Login - AD Messaging App</title>
<script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet"/>
<style>
body {
font-family: 'Inter', sans-serif;
}
</style>
</head>
<body class="bg-background-dark text-text-dark flex items-center justify-center h-screen">
<div class="w-full max-w-md p-8 space-y-8 bg-input-bg-dark rounded-xl shadow-lg">
<h2 class="text-2xl font-bold text-center">Log in to your account</h2>
<?php if ($error): ?>
<div class="p-4 text-sm text-red-700 bg-red-100 rounded-lg" role="alert">
<?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<form method="POST" action="login.php" class="space-y-6">
<div>
<label for="username" class="text-sm font-medium text-gray-300">Username</label>
<input id="username" name="username" type="text" required
class="w-full px-4 py-2 mt-2 text-white bg-gray-700 border border-gray-600 rounded-lg focus:ring-primary focus:border-primary">
</div>
<div>
<label for="password" class="text-sm font-medium text-gray-300">Password</label>
<input id="password" name="password" type="password" required
class="w-full px-4 py-2 mt-2 text-white bg-gray-700 border border-gray-600 rounded-lg focus:ring-primary focus:border-primary">
</div>
<div>
<button type="submit"
class="w-full px-4 py-2 font-semibold text-white bg-primary rounded-lg hover:bg-primary-dark focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary dark:focus:ring-offset-background-dark">
Log in
</button>
</div>
</form>
<p class="text-sm text-center text-gray-400">
Don't have an account? <a href="register.php" class="font-medium text-primary hover:underline">Register</a>
</p>
</div>
</body>
</html>