Files
Unimarket/auth/login.php
T
2025-06-23 21:41:03 +08:00

85 lines
2.9 KiB
PHP

<?php
require_once '../includes/auth_functions.php';
if (isLoggedIn()) {
header("Location: /unimarket/index.php");
exit();
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_once '../config/database.php';
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (empty($username) || empty($password)) {
$error = 'Please fill in all fields.';
} else {
$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['user_id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
if ($user['role'] === 'admin') {
header("Location: /unimarket/admin/dashboard.php");
} elseif ($user['role'] === 'owner') {
header("Location: /unimarket/owner/dashboard.php");
} else {
header("Location: /unimarket/index.php");
}
exit();
} else {
$error = 'Invalid username or password.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - UniMarket</title>
<link rel="stylesheet" href="/unimarket/assets/css/style.css">
<link rel="stylesheet" href="/unimarket/assets/css/responsive.css">
</head>
<body>
<?php include '../includes/header.php'; ?>
<div class="container">
<div class="card" style="max-width: 500px; margin: 50px auto;">
<h1 style="text-align: center; margin-bottom: 20px;">Login to UniMarket</h1>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo $error; ?></div>
<?php endif; ?>
<form action="" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit" class="btn">Login</button>
</form>
<p style="text-align: center; margin-top: 20px;">
Don't have an account? <a href="/unimarket/auth/register.php">Register here</a>
</p>
</div>
</div>
<?php include '../includes/footer.php'; ?>
</body>
</html>