-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_dashboard.php
67 lines (56 loc) · 1.84 KB
/
admin_dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
session_start();
if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'admin') {
header('Location: index.php');
exit();
}
include 'config.php';
// Delete User Logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deleteUser'])) {
$usernameToDelete = $_POST['deleteUser'];
// Perform user deletion only for regular users
$stmt = $pdo->prepare("DELETE FROM users WHERE username = ? AND role = 'regular'");
$stmt->execute([$usernameToDelete]);
echo 'User deleted successfully.';
}
// Logout Logic
if (isset($_POST['logout'])) {
session_unset();
session_destroy();
header('Location: index.php');
exit();
}
// Fetch all users
$stmt = $pdo->prepare("SELECT * FROM users");
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ... your HTML head content ... -->
</head>
<body>
<h2>Welcome, Admin!</h2>
<h3>User List:</h3>
<ul>
<?php foreach ($users as $user): ?>
<li>
<?php echo $user['username']; ?> (Role: <?php echo $user['role']; ?>)
<?php if ($user['role'] === 'regular'): ?>
<form action="admin_dashboard.php" method="post" style="display:inline;">
<input type="hidden" name="deleteUser" value="<?php echo $user['username']; ?>">
<button type="submit">Delete</button>
</form>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<!-- Logout link -->
<form action="admin_dashboard.php" method="post">
<input type="hidden" name="logout">
<button type="submit">Logout</button>
</form>
<!-- ... rest of your admin dashboard content ... -->
</body>
</html>