37650-vm/login.php
Flatlogic Bot 9c65e16259 sad
2026-01-21 14:05:59 +00:00

132 lines
5.7 KiB
PHP

<?php
require_once 'db/config.php';
session_start();
if (isset($_SESSION['user_id'])) {
// Redirect to the appropriate dashboard if already logged in
if (isset($_SESSION['role']) && $_SESSION['role'] === 'admin') {
header("Location: admin/index.php");
} else {
header("Location: dashboard.php");
}
exit();
}
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if (empty($username)) {
$errors[] = 'Username is required.';
}
if (empty($password)) {
$errors[] = 'Password is required.';
}
if (empty($errors)) {
try {
$pdo = db();
// Allow login by username only per new schema
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username LIMIT 1");
$stmt->execute(['username' => $username]);
$user = $stmt->fetch();
// Note: The 'password' column stores the hash
if ($user && password_verify($password, $user['password'])) {
if (isset($user['status']) && $user['status'] !== 'active' && isset($user['status'])) {
// Status column is not in the mandatory schema but might remain if I didn't drop it?
// The prompt asked for specific columns. I will assume only those columns exist.
// So I should probably remove the status check unless I add status to the schema.
// The prompt schema for users: id, username, password, role, created_at. NO STATUS.
// I will remove the status check to be safe.
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
// Redirect to the appropriate dashboard
if ($user['role'] === 'admin') {
header("Location: admin/index.php");
} else {
header("Location: dashboard.php");
}
exit();
} else {
// If status column exists and is checked above...
// Re-implementing logic:
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
if ($user['role'] === 'admin') {
header("Location: admin/index.php");
} else {
header("Location: dashboard.php");
}
exit();
}
} else {
$errors[] = 'Invalid login credentials.';
}
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
$errors[] = "An internal error occurred. Please try again later.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Car Sells in Afghanistan</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="form-container">
<div class="card auth-card shadow-lg border-0">
<div class="card-body p-5">
<div class="text-center mb-5">
<h1 class="h2 fw-bold">Welcome Back!</h1>
<p class="text-muted">Login to access your account.</p>
</div>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger" role="alert">
<?php foreach ($errors as $error): ?>
<p class="mb-0 small"><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="login.php" method="POST">
<div class="mb-4">
<label for="username" class="form-label fw-semibold">Username</label>
<input type="text" id="username" name="username" class="form-control form-control-lg" placeholder="admin" required value="<?php echo isset($username) ? htmlspecialchars($username) : ''; ?>">
</div>
<div class="mb-4">
<label for="password" class="form-label fw-semibold">Password</label>
<input type="password" id="password" name="password" class="form-control form-control-lg" placeholder="••••••••" required>
</div>
<div class="d-grid mt-5">
<button type="submit" class="btn btn-primary btn-lg shadow-sm">Login</button>
</div>
</form>
<p class="text-center text-muted mt-4">
Don't have an account? <a href="register.php" class="text-primary text-decoration-none fw-semibold">Create one now</a>.
</p>
<p class="text-center text-muted mt-2">
<a href="index.php" class="text-muted text-decoration-none small"><i class="bi bi-arrow-left"></i> Back to Home</a>
</p>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>