This commit is contained in:
Flatlogic Bot 2025-10-07 23:57:03 +00:00
parent 82916fbf7a
commit 7181333ac7
8 changed files with 97 additions and 22 deletions

View File

@ -1,19 +1,24 @@
<?php <?php
session_start(); session_start();
require_once 'includes/auth.php';
check_login();
if (!isset($_SESSION['user_id'])) { if (isset($_SESSION['user_role'])) {
$role = $_SESSION['user_role'];
if ($role == 'donor') {
header("Location: donor_dashboard.php");
exit;
} elseif ($role == 'ngo') {
header("Location: ngo_dashboard.php");
exit;
} else {
// Fallback for other roles, e.g. super_admin
// For now, just a simple message.
echo "Welcome, you are logged in!";
}
} else {
// Should not happen if check_login() is working correctly
header("Location: login.php"); header("Location: login.php");
exit; exit;
} }
?>
require_once 'includes/header.php';
?>
<div class="container mt-5">
<h1>Welcome to your Dashboard</h1>
<p>You are logged in as a <strong><?php echo htmlspecialchars($_SESSION['user_role']); ?></strong>.</p>
<p>This is a placeholder for your dashboard content.</p>
<a href="logout.php">Logout</a>
</div>
<?php require_once 'includes/footer.php'; ?>

16
donor_dashboard.php Normal file
View File

@ -0,0 +1,16 @@
<?php
session_start();
require_once 'includes/auth.php';
check_login();
check_role('donor');
require_once 'includes/header.php';
?>
<div class="container mt-5">
<h1>Welcome, Donor!</h1>
<p>This is your dashboard. Here you can manage your profile and donations.</p>
<a href="logout.php" class="btn btn-danger">Logout</a>
</div>
<?php require_once 'includes/footer.php'; ?>

17
includes/auth.php Normal file
View File

@ -0,0 +1,17 @@
<?php
function check_login() {
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
}
function check_role($role) {
if ($_SESSION['user_role'] !== $role) {
// Redirect to their own dashboard if they try to access another role's page
$dashboard = $_SESSION['user_role'] . '_dashboard.php';
header("Location: $dashboard");
exit;
}
}
?>

View File

@ -37,12 +37,21 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="index.php#contact">Contact</a> <a class="nav-link" href="index.php#contact">Contact</a>
</li> </li>
<li class="nav-item"> <?php if (isset($_SESSION['user_id'])): ?>
<a class="btn btn-outline-primary ms-2" href="login.php">Login</a> <li class="nav-item">
</li> <a class="btn btn-outline-primary ms-2" href="<?php echo $_SESSION['user_role']; ?>_dashboard.php">Dashboard</a>
<li class="nav-item"> </li>
<a class="btn btn-primary ms-2" href="register.php">Register</a> <li class="nav-item">
</li> <a class="btn btn-primary ms-2" href="logout.php">Logout</a>
</li>
<?php else: ?>
<li class="nav-item">
<a class="btn btn-outline-primary ms-2" href="login.php">Login</a>
</li>
<li class="nav-item">
<a class="btn btn-primary ms-2" href="register.php">Register</a>
</li>
<?php endif; ?>
</ul> </ul>
</div> </div>
</div> </div>

View File

@ -14,7 +14,13 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($user && password_verify($password, $user['password'])) { if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id']; $_SESSION['user_id'] = $user['id'];
$_SESSION['user_role'] = $user['role']; $_SESSION['user_role'] = $user['role'];
header("Location: dashboard.php"); if ($user['role'] == 'donor') {
header("Location: donor_dashboard.php");
} elseif ($user['role'] == 'ngo') {
header("Location: ngo_dashboard.php");
} else {
header("Location: dashboard.php"); // Fallback for other roles
}
exit; exit;
} else { } else {
$error = "Invalid credentials"; $error = "Invalid credentials";

View File

@ -2,5 +2,5 @@
session_start(); session_start();
session_unset(); session_unset();
session_destroy(); session_destroy();
header("Location: login.php"); header("Location: index.php");
exit; exit;

16
ngo_dashboard.php Normal file
View File

@ -0,0 +1,16 @@
<?php
session_start();
require_once 'includes/auth.php';
check_login();
check_role('ngo');
require_once 'includes/header.php';
?>
<div class="container mt-5">
<h1>Welcome, NGO!</h1>
<p>This is your dashboard. Here you can manage your profile and view donation requests.</p>
<a href="logout.php" class="btn btn-danger">Logout</a>
</div>
<?php require_once 'includes/footer.php'; ?>

View File

@ -24,7 +24,13 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($stmt->execute([$email, $hashed_password, $role])) { if ($stmt->execute([$email, $hashed_password, $role])) {
$_SESSION['user_id'] = $pdo->lastInsertId(); $_SESSION['user_id'] = $pdo->lastInsertId();
$_SESSION['user_role'] = $role; $_SESSION['user_role'] = $role;
header("Location: dashboard.php"); if ($role == 'donor') {
header("Location: donor_dashboard.php");
} elseif ($role == 'ngo') {
header("Location: ngo_dashboard.php");
} else {
header("Location: dashboard.php");
}
exit; exit;
} else { } else {
$error = "Registration failed"; $error = "Registration failed";