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

highscore.php

102 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";

$invalidRequest false;
if (isset(
$_GET['q'])) {

 include 
"dbconnection.php";

 
$quizId $_GET['q'];

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

 if (empty(
$quizInfo)) {
  
$invalidRequest true;
 } else {

  
$sql "
  SELECT 
   result_id,
   quiz_users.displayname,
   quiz_id,
   user_points,
   max_points 
  FROM quiz_results 
  INNER JOIN quiz_users ON quiz_results.user_id = quiz_users.user_id 
  WHERE quiz_id=? 
  ORDER BY user_points DESC, displayname ASC"
;

  
$stmt $dbconn->prepare($sql);
  
$stmt->execute([$quizId]);
  
$results $stmt->fetchAll(PDO::FETCH_ASSOC);
 }
} else {
 
$invalidRequest true;
}

if (
$invalidRequest) {
 
header("Location:quizzes.php");
}

?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Highscores for quiz <?= $quizInfo['quiz_name']?> - 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>

 <h1>Highscores for quiz <span class='cursive'><?= $quizInfo['quiz_name'?></h1>

 <main>
  <a href="quizzes.php">return to quiz list</a><br>
  <table>
   <tr>
    <th>Rank</th>
    <th>User</th>
    <th>Points<?= count($results) > " (out of {$results[0]['max_points']})" "" ?></th>
   </tr>
  <?php
  
foreach ($results as $index => $resultInfo) {
   
// if points are the same as previous result
   
if (isset($results[$index 1]) && $resultInfo['user_points'] == $results[$index 1]['user_points']) {
    
// the previous rank is then used again
   
} else {
    
$rank $index 1;
   }

   
?>
   <tr>
    <td><?= $rank ?></td>
    <td><?= $resultInfo['displayname'?></td>
    <td><?= $resultInfo['user_points'?> points</td>
   </tr>
  <?php }
   
   if (
count($results) < 1) { ?>
    <td colspan="3">No results to show</td><?php
   
?>
  </table>
 </main>
</body>
</html>