Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/projekt-quiz/admin/
admin_login.php
admin_manage_quiz.php
admin_manage_users.php
admin_panel.php
admin_statistics.php
make_admin.php
make_admin.php
104 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
<?php
/**
* Utility script to make a user an admin.
* Run this ONCE, then delete it for security.
*
* Usage: Navigate to this file in your browser, enter the username to make admin.
*/
session_start();
require_once '../includes/db_connect.php';
$message = '';
$users = [];
// Fetch all users to show in a list
$stmt = $pdo->query("SELECT id, name, username, is_admin FROM users ORDER BY username");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['user_id'])) {
$user_id = intval($_POST['user_id']);
$stmt = $pdo->prepare("UPDATE users SET is_admin = 1 WHERE id = :id");
$stmt->execute([':id' => $user_id]);
if ($stmt->rowCount() > 0) {
$message = "<p style='color:green;'>✓ Användare med ID $user_id är nu admin!</p>";
// Refresh user list
$stmt = $pdo->query("SELECT id, name, username, is_admin FROM users ORDER BY username");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
$message = "<p style='color:red;'>Ingen användare hittades med det ID:t.</p>";
}
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Gör användare till admin</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
table { border-collapse: collapse; margin: 20px 0; }
th, td { border: 1px solid #ccc; padding: 10px; text-align: left; }
th { background: #f0f0f0; }
.is-admin { color: green; font-weight: bold; }
.not-admin { color: gray; }
button { padding: 5px 15px; cursor: pointer; }
.warning { background: #fff3cd; padding: 15px; border: 1px solid #ffc107; margin-bottom: 20px; }
</style>
</head>
<body>
<h1>Gör användare till admin</h1>
<div class="warning">
<strong>⚠️ Varning:</strong> Radera denna fil (make_admin.php) när du är klar för säkerhets skull!
</div>
<?php echo $message; ?>
<?php if (empty($users)): ?>
<p>Inga användare hittades i databasen. Registrera en användare först.</p>
<?php else: ?>
<h2>Alla användare:</h2>
<table>
<thead>
<tr>
<th>ID</th>
<th>Namn</th>
<th>Användarnamn</th>
<th>Admin?</th>
<th>Åtgärd</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo htmlspecialchars($user['name']); ?></td>
<td><?php echo htmlspecialchars($user['username']); ?></td>
<td class="<?php echo $user['is_admin'] ? 'is-admin' : 'not-admin'; ?>">
<?php echo $user['is_admin'] ? 'Ja ✓' : 'Nej'; ?>
</td>
<td>
<?php if (!$user['is_admin']): ?>
<form method="POST" style="margin:0;">
<input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
<button type="submit">Gör till admin</button>
</form>
<?php else: ?>
<span style="color:green;">Redan admin</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<p><a href="admin_login.php">← Tillbaka till admin-inloggning</a></p>
</body>
</html>