Webbserverprogrammering 1

Show sourcecode

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

ramverket/exercises/fragetavling/

ovn_fragetavling3.php
ovn_fragetavling4.php

ovn_fragetavling4.php

113 lines UTF-8 Windows (CRLF)
<?php
  
// Title: Frågetävling 4
  
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

  
$current_phase 1;
  
$username '';
  
$answers = [];
  
$cheat_sheet = ['a''b''c'];
  
$password 'jail';
?>

<!DOCTYPE html>
<html lang="sv">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Frågetävling 4</title>
</head>
<body>
  <?php
    
if (isset($_POST['password']) && $_POST['password'] !== $password && $current_phase == 1) {
      echo 
'Fel lösenord! Åtkomst nekad!';
      return;
    }

    if (isset(
$_POST['current_phase'])) {
      
$current_phase number_format($_POST['current_phase']); 
    } else {
      
$current_phase 1;
    }

    if (isset(
$_POST['answers']) && $_POST['answers'] !== '') {
      
$answers explode(','$_POST['answers']);
    } else {
      
$answers = [];
    }

    if (isset(
$_POST['name'])) {
      
$username $_POST['name'];
    } else {
      
$username '';
    }

    if (isset(
$_POST['q1'])) {
      
array_push($answers$_POST['q1']);
    }
    if (isset(
$_POST['q2'])) {
      
array_push($answers$_POST['q2']);
    }
    if (isset(
$_POST['q3'])) {
      
array_push($answers$_POST['q3']);
    }
  
?>
  <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="POST">
    <?php if ($current_phase == 1) : ?>
      <input type="hidden" name="current_phase" value="2">
      <input type="hidden" name="answers" value="<?php echo htmlspecialchars(implode(','$answers)); ?>">
      <input type="text" name="name" required placeholder="Ditt namn">
      <input type="password" name="password" required placeholder="Lösenord">
      <button type="submit" value="2">Nästa</button>
    <?php endif; ?>
    <?php if ($current_phase == 2) : ?>
      <input type="hidden" name="current_phase" value="3">
      <input type="hidden" name="answers" value="<?php echo htmlspecialchars(implode(','$answers)); ?>">
      <input type="hidden" name="name" value="<?php echo htmlspecialchars($username); ?>">
      <p id="q1">Vilket språk används främst för att strukturera innehåll på webbsidor?</p>
      <input type="radio" name="q1" value="a" required id="q1a"><label for="q1a">HTML</label>
      <input type="radio" name="q1" value="b" required id="q1b"><label for="q1b">CSS</label>
      <input type="radio" name="q1" value="c" required id="q1c"><label for="q1c">JavaScript</label>
      <input type="radio" name="q1" value="d" required id="q1d"><label for="q1d">PHP</label>
      <button type="submit" value="3">Nästa</button>
    <?php endif; ?>
    <?php if ($current_phase == 3) : ?>
      <input type="hidden" name="current_phase" value="4">
      <input type="hidden" name="answers" value="<?php echo htmlspecialchars(implode(','$answers)); ?>">
      <input type="hidden" name="name" value="<?php echo htmlspecialchars($username); ?>">
      <p id="q2">Vad används CSS huvudsakligen till?</p>
      <input type="radio" name="q2" value="a" id="q2a"><label for="q2a">Att skapa serverlogik</label>
      <input type="radio" name="q2" value="b" id="q2b"><label for="q2b">Att styra layout och design</label>
      <input type="radio" name="q2" value="c" id="q2c"><label for="q2c">Att lagra data i databaser</label>
      <input type="radio" name="q2" value="d" id="q2d"><label for="q2d">Att strukturera textinnehåll</label>
      <button type="submit" value="4">Nästa</button>
    <?php endif; ?>
    <?php if ($current_phase == 4) : ?>
      <input type="hidden" name="current_phase" value="5">
      <input type="hidden" name="answers" value="<?php echo htmlspecialchars(implode(','$answers)); ?>">
      <input type="hidden" name="name" value="<?php echo htmlspecialchars($username); ?>">
      <p id="q3">Vilken av följande tekniker är en JavaScript-baserad frontend-ramverk?</p>
      <input type="radio" name="q3" value="a" id="q3a"><label for="q3a">Laravel</label>
      <input type="radio" name="q3" value="b" id="q3b"><label for="q3b">Django</label>
      <input type="radio" name="q3" value="c" id="q3c"><label for="q3c">React</label>
      <input type="radio" name="q3" value="d" id="q3d"><label for="q3d">Flask</label>
      <button type="submit" value="Skicka">Skicka</button>
    <?php endif; ?>
    <?php if ($current_phase == 5) : ?>
      <?php
        $total 
count($cheat_sheet);
        
$correct 0;
        foreach (
$answers as $i => $ans) {
          if (
$ans === $cheat_sheet[$i]) {
            
$correct++;
          }
        }
      
?>
      <h1>Tack för dina svar, <?php echo htmlspecialchars($username); ?>!</h1>
      <p>Ditt resultat: <?php echo htmlspecialchars("$correct/$total"); ?></p>
      <?php mail("pavvor23@varmdogymnasium.se"htmlspecialchars("Resultat för spelaren (uppg 4) - $username"), htmlspecialchars("$correct/$total")); ?>
    <?php endif; ?>
  </form>
</body>
</html>