Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/projects/quiz/

22 filer

admin.php
create_account.php
create_quiz.php
css/
dbconnection.php
hacktest.php
highscore.php
index.php
js/
login.php
mysql_create_table_options.php
mysql_create_table_questions.php
mysql_create_table_quizzes.php
mysql_create_table_results.php
mysql_create_table_submits.php
mysql_create_table_users.php
planering.txt
profile.php
quizzes.php
resources/
result.php
session_variable_array_check.php

admin.php

144 lines ASCII Windows (CRLF)
<?php

error_reporting
(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly

function dump($dump) {
 echo 
"<pre>";
 
var_dump($dump);
 echo 
"</pre>";
}

session_start();
include 
"session_variable_array_check.php";

if (!
$_SESSION['quiz']['loggedIn']) {
 
header("Location:login.php");
}
if (!
$_SESSION['quiz']['admin']) {
 
header("Location:index.php");
}

include 
"dbconnection.php";

if (isset(
$_POST['deleteQuiz'])) {
 
$deleteQuizId = (int) $_POST['deleteQuiz'];

 
// delete specified quiz as well as questions and options of that quiz 

 
$tables = ["quiz_quizzes""quiz_questions""quiz_options"];

 foreach (
$tables as $tableName) {
  
$sql "DELETE FROM {$tableName} WHERE quiz_id=?";
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute([$deleteQuizId]);
 }
}
if (isset(
$_POST['deleteUser'])) {
 
$deleteUserId = (int) $_POST['deleteUser'];

 
// delete specified user

 
$sql "DELETE FROM quiz_users WHERE user_id=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$deleteUserId]);
}

// select all quizzes for showing in table
$sql "SELECT * FROM quiz_quizzes";
$stmt $dbconn->prepare($sql);
$stmt->execute();
$quizzes $stmt->fetchAll(PDO::FETCH_ASSOC);

// select all users
$sql "SELECT user_id, displayname, username, user_level FROM quiz_users";
$stmt $dbconn->prepare($sql);
$stmt->execute();
$users $stmt->fetchAll(PDO::FETCH_ASSOC PDO::FETCH_UNIQUE);


?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Administration</title>
 <meta charset="utf-8">
 <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
 <a href="index.php" id="logo">
  CuriousQuizzes
 </a>
 <h1>Administration</h1>

 <?php
 
if (isset($quizId)) { ?>
  <h4>Successfully deleted quiz! (ID: <?= $quizId ?>)</h4><?php
 
?>

 <main class="wideMain">
  <table>
   <tr>
    <th>Quiz Name</th>
    <th>Creator</th>
    <th>Question count</th>
    <th>Link</th>
    <th>Delete</th>
   </tr>
   <?php
   
foreach ($quizzes as $quizInfo) {?>
   <tr>
    <td><?= $quizInfo['quiz_name'?></td>
    <td><?php
     
if (isset($users[$quizInfo['creator_user_id']])) {
      
$userId $quizInfo['creator_user_id'];
      echo 
"<a class='user' href='profile.php?user={$userId}'>{$users[$userId]['displayname']}</a>";
     } else {
      echo 
"<span class='cursive'>deleted user</span>";
     }
     
?></td>
    <td><?= $quizInfo['nr_of_questions'?> questions</td>
    <td><a href="quizzes.php?q=<?= $quizInfo['quiz_id'?>">run quiz</a></td>
    <td class="centerCell">
     <form method="post" action="">
      <input type="hidden" name="deleteQuiz" value="<?= $quizInfo['quiz_id'?>">
      <input type="submit" name="deleteQuizSubmit" value="delete">
     </form>
    </td>
   </tr>
   <?php }
   if (
count($quizzes) < 1) { ?>
    <td colspan="5">No quizzes to show</td><?php
   
?>
  </table>

  <table>
   <tr>
    <th>Display name</th>
    <th>Username</th>
    <th>User level</th>
    <th>Delete</th>
   </tr>
   <?php
   
foreach ($users as $userId => $user) {?>
   <tr>
    <td><?= $user['displayname'?></td>
    <td><?= $user['username'?></td>
    <td><?= $user['user_level'?></td>
    <td class="centerCell">
     <form method="post" action="">
      <input type="hidden" name="deleteUser" value="<?= $userId ?>">
      <input type="submit" name="deleteUserSubmit" value="delete">
     </form>
    </td>
   </tr>
   <?php }
   if (
count($quizzes) < 1) { ?>
    <td colspan="4">No quizzes to show</td><?php
   
?>
  </table>
 </main>
</body>
</html>