Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserverprogrammering/submissions/projekt-quiz/

README.md
admin/
create_admin.php
create_quiz.php
css/
dashboard.php
debug_questions.php
includes/
index.php
leaderboard.php
login.php
logout.php
my_results.php
quiz.php
quiz_result.php
register.php
sqlcredentials

create_admin.php

100 lines UTF-8 Windows (CRLF)
<?php
/**
 * Create Admin User with Hashed Password
 * DELETE THIS FILE AFTER USE!
 */

session_start();
require_once 
'includes/db_connect.php';

$message '';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
    
$name trim($_POST['name']);
    
$username trim($_POST['username']);
    
$password $_POST['password'];
    
    if (empty(
$name) || empty($username) || empty($password)) {
        
$message "<p style='color:red;'>Alla fält måste fyllas i.</p>";
    } else {
        
// Check if username already exists
        
$stmt $pdo->prepare("SELECT id FROM users WHERE username = :username");
        
$stmt->execute([':username' => $username]);
        
        if (
$stmt->fetch()) {
            
// User exists - update to admin with new hashed password
            
$hashed_password password_hash($passwordPASSWORD_DEFAULT);
            
$stmt $pdo->prepare("UPDATE users SET name = :name, password = :password, is_admin = 1 WHERE username = :username");
            
$stmt->execute([
                
':name' => $name,
                
':password' => $hashed_password,
                
':username' => $username
            
]);
            
$message "<p style='color:green;'>✓ Användare '$username' uppdaterad till admin med hashat lösenord!</p>";
        } else {
            
// Create new admin user
            
$hashed_password password_hash($passwordPASSWORD_DEFAULT);
            
$stmt $pdo->prepare("
                INSERT INTO users (name, username, password, is_admin, created_at) 
                VALUES (:name, :username, :password, 1, NOW())
            "
);
            
$stmt->execute([
                
':name' => $name,
                
':username' => $username,
                
':password' => $hashed_password
            
]);
            
$message "<p style='color:green;'>✓ Admin-användare '$username' skapad med hashat lösenord!</p>";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Skapa Admin</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; max-width: 500px; }
        .warning { background: #f8d7da; padding: 15px; border: 1px solid #f5c6cb; margin-bottom: 20px; color: #721c24; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; font-weight: bold; }
        input { width: 100%; padding: 10px; font-size: 16px; box-sizing: border-box; }
        button { padding: 12px 25px; font-size: 16px; background: #007bff; color: white; border: none; cursor: pointer; }
        button:hover { background: #0056b3; }
    </style>
</head>
<body>
    <h1>Skapa Admin-användare</h1>
    
    <div class="warning">
        <strong>⚠️ RADERA DENNA FIL NÄR DU ÄR KLAR!</strong>
    </div>
    
    <?php echo $message?>
    
    <form method="POST">
        <div class="form-group">
            <label for="name">Namn:</label>
            <input type="text" id="name" name="name" required placeholder="Admin Namn">
        </div>
        
        <div class="form-group">
            <label for="username">Användarnamn:</label>
            <input type="text" id="username" name="username" required placeholder="admin">
        </div>
        
        <div class="form-group">
            <label for="password">Lösenord (kommer hashas):</label>
            <input type="password" id="password" name="password" required placeholder="Ditt lösenord">
        </div>
        
        <button type="submit">Skapa/Uppdatera Admin</button>
    </form>
    
    <p style="margin-top: 20px;">
        <a href="admin/admin_login.php">→ Gå till admin-inloggning</a>
    </p>
</body>
</html>