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

result.php

194 lines UTF-8 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 (!isset(
$_POST['submitQuiz']) && !isset($_GET['r']))
 
header("Location:quizzes.php");

include 
"dbconnection.php";

$usingGet true;
$success true;
$answers = [];

if (isset(
$_POST['submitQuiz'])) {
 
$usingGet false;

 
$quizId = (int) $_POST['quizId'];
 
$displayname $_SESSION['quiz']['displayname']; // empty if not logged in

 // retrieves user's answered option for each question
 
foreach ($_POST as $questionId => $answeredOptionId) {
  if (
is_int($questionId)) {
   
$answers[$questionId] = $answeredOptionId;
  }
 }

 
// retrieves array of the correct options for this quiz. Index is question id
 
$sql "SELECT question_id, option_id FROM quiz_options WHERE quiz_id=? AND correct=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$quizId"correct"]);
 
$correctOptions $stmt->fetchAll(PDO::FETCH_GROUP PDO::FETCH_ASSOC PDO::FETCH_UNIQUE);

 
$userPoints 0;
 
$maxPoints count($correctOptions);

 foreach (
$correctOptions as $questionId => $correctOption) {
  if (
$answers[$questionId] == $correctOption['option_id']) {
   
// answered correct for question $questionId
   
$userPoints++;
  }
 }

 
$sql "SELECT quiz_name FROM quiz_quizzes WHERE quiz_id=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$quizId]);
 
$quizInfo $stmt->fetch(PDO::FETCH_ASSOC);

 
$quizName $quizInfo['quiz_name'];

 
// registers result information into database only if logged in
 
if ($_SESSION['quiz']['loggedIn']) {

  
$userId $_SESSION['quiz']['userId'];

  
// for one quiz try
  
$sql "INSERT INTO quiz_results (user_id, quiz_id, user_points, max_points) VALUES (?, ?, ?, ?)";
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute([$userId$quizId$userPoints$maxPoints]);
  
  
$resultId = (int) $dbconn->lastInsertId();
  
  
// for each question/answer submitted
  
foreach ($answers as $questionId => $optionId) {
   
$sql "INSERT INTO quiz_submits (user_id, quiz_id, result_id, question_id, option_id) VALUES (?, ?, ?, ?, ?)";
   
$stmt $dbconn->prepare($sql);
   
$stmt->execute([$userId$quizId$resultId$questionId, ((int) $optionId)]);
  }
 }

} else {

 
$resultId $_GET['r'];

 
$sql "SELECT question_id, option_id FROM quiz_submits WHERE result_id=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$resultId]);
 
$answersLayered $stmt->fetchAll(PDO::FETCH_ASSOC PDO::FETCH_UNIQUE PDO::FETCH_GROUP);

 
// if $answersLayered is empty, means that provided result id is invalid
 
if (!empty($answersLayered)) {
  
  
// means result id was functional, thus can now be used safely

  
foreach ($answersLayered as $questionId => $answerInfo) {
   
$answers[$questionId] = $answerInfo['option_id'];
  }

  
$sql "
  SELECT 
   quiz_users.displayname, 
   quiz_results.quiz_id, 
   quiz_quizzes.quiz_name, 
   user_points, 
   max_points 
  FROM quiz_results 
  INNER JOIN quiz_users ON quiz_results.user_id = quiz_users.user_id 
  INNER JOIN quiz_quizzes ON quiz_results.quiz_id = quiz_quizzes.quiz_id 
  WHERE result_id=?"
;
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute([$resultId]);
  
$result $stmt->fetch(PDO::FETCH_ASSOC);

  
$displayname $result['displayname'];
  
$quizId $result['quiz_id'];
  
$quizName $result['quiz_name'];
  
$userPoints $result['user_points'];
  
$maxPoints $result['max_points'];

 } else {
  
$success false;
 }
}

if (
$success) {
 
// all options for quiz (for printout)
 
$sql "SELECT question_id, option_id, option_txt, correct FROM quiz_options WHERE quiz_id=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$quizId]);
 
$allOptions $stmt->fetchAll(PDO::FETCH_GROUP PDO::FETCH_ASSOC);

 
// all questions for quiz (for printout)
 
$sql "SELECT question_id, question_nr, question_txt FROM quiz_questions WHERE quiz_id=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$quizId]);
 
$questions $stmt->fetchAll(PDO::FETCH_ASSOC PDO::FETCH_UNIQUE);
}

?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Result - CuriousQuizzes</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> <?php
if ($success) { 

 if (
$usingGet) { ?>
  <h1>You are looking at an old result:</h1>
 <?php ?>
 <h1>Result for user <?= $displayname ?> for quiz <span class="cursive"><?= $quizName ?></span></h1>
 <main>
  <?php
  
if (!$usingGet) { ?>
   <a href="quizzes.php">return to quiz list</a>
  <?php ?>
  <h3 class="pointsResult"><?= $userPoints ?> out of <?= $maxPoints ?> correct!</h3>
  
  <?php

  
foreach ($questions as $questionId => $question) {

   
?><h4><?= "Question {$question['question_nr']}?></h4>
   <p class="correctedQuestion"><?= $question['question_txt'?></p><?php

   
foreach ($allOptions[$questionId] as $option) {

    
$classList = ["correctedOption"];
    if (
$answers[$questionId] == $option['option_id']) // user answered this option
     
$classList[] = "answered"// set background to red
    
if ($option['correct'] === "correct"// this option is correct
     
$classList[] = "correct"// set background to green

    
?><p class="<?= join(" "$classList?>"><input type="radio" disabled><?= $option['option_txt'?></p><?php
   
}
  } 
?>
 </main>
<?php } else { ?>
 <h1>Invalid argument. Broken link!</h1>
 <h1>¯\_(ツ)_/¯</h1>
 <main>
  bringing you back in 5 seconds...<br><br>
 </main>
 <script type="text/javascript">
  setTimeout(() => window.history.back(), 5000);
 </script>
<?php ?>
</body>
</html>