Webbserverprogrammering 1

Show sourcecode

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

ramverket/exercises/cookie-session/

ovn_cookie-session1.php
ovn_cookie-session2.php
ovn_cookie-session3.php
ovn_cookie-session4.php
ovn_cookie-session5.php

ovn_cookie-session5.php

119 lines UTF-8 Windows (CRLF)
<?php
  
// Title: Cookie-Session 5
  
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

  
session_start();

  if (!isset(
$_SESSION['current_phase'])) $_SESSION['current_phase'] = 1;
  if (!isset(
$_SESSION['username'])) $_SESSION['username'] = '';
  if (!isset(
$_SESSION['answers'])) $_SESSION['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>Cookie-Session 5</title>
</head>
<body>
  <?php
    
if (isset($_POST['password'])) {
      
$_SESSION['password'] = $_POST['password'];
      
$_SESSION['current_phase'] = 2;
    }

    if (isset(
$_SESSION['password']) && $_SESSION['password'] !== $password) {
      
session_unset();
      
session_destroy();
      
header('Refresh: 0');
      return;
    }

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

    if (isset(
$_POST['q1'])) {
      
$_SESSION['current_phase'] = 3;
      
array_push($_SESSION['answers'], $_POST['q1']);
    } else if (isset(
$_POST['q2'])) {
      
$_SESSION['current_phase'] = 4;
      
array_push($_SESSION['answers'], $_POST['q2']);
    } else if (isset(
$_POST['q3'])) {
      
$_SESSION['current_phase'] = 5;
      
array_push($_SESSION['answers'], $_POST['q3']);
    }
  
?>
  <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="POST">
    <?php if ($_SESSION['current_phase'] == 1) : ?>
      <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 ($_SESSION['current_phase'] == 2) : ?>
      <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 ($_SESSION['current_phase'] == 3) : ?>
      <p id="q2">Vad används CSS huvudsakligen till?</p>
      <input type="radio" name="q2" value="a" required id="q2a"><label for="q2a">Att skapa serverlogik</label>
      <input type="radio" name="q2" value="b" required id="q2b"><label for="q2b">Att styra layout och design</label>
      <input type="radio" name="q2" value="c" required id="q2c"><label for="q2c">Att lagra data i databaser</label>
      <input type="radio" name="q2" value="d" required id="q2d"><label for="q2d">Att strukturera textinnehåll</label>
      <button type="submit" value="4">Nästa</button>
    <?php endif; ?>
    <?php if ($_SESSION['current_phase'] == 4) : ?>
      <p id="q3">Vilken av följande tekniker är ett JavaScript-baserat frontend-ramverk?</p>
      <input type="radio" name="q3" value="a" required id="q3a"><label for="q3a">Laravel</label>
      <input type="radio" name="q3" value="b" required id="q3b"><label for="q3b">Django</label>
      <input type="radio" name="q3" value="c" required id="q3c"><label for="q3c">React</label>
      <input type="radio" name="q3" value="d" required id="q3d"><label for="q3d">Flask</label>
      <button type="submit" value="Skicka">Skicka</button>
    <?php endif; ?>
    <?php if ($_SESSION['current_phase'] == 5) : ?>
      <?php
        $total 
count($cheat_sheet);
        
$correct 0;
        foreach (
$_SESSION['answers'] as $i => $ans) {
          if (
$ans === $cheat_sheet[$i]) {
            
$correct++;
          }
        }
      
?>
      <h1>Tack för dina svar, <?php echo $_SESSION['username']; ?>!</h1>
      <p>Ditt resultat: <?php echo htmlspecialchars("$correct/$total"); ?></p>
      <hr>
      <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" <?php if ($_SESSION['answers'][0] === "a") echo "checked"?> disabled><label for="q1a" <?php if ($cheat_sheet[0] === "a") echo "style = \"color: green;\""; else if ($_SESSION['answers'][0] === "a") echo "style = \"color: red;\""?> >HTML</label>
      <input type="radio" name="q1" value="b" required id="q1b" <?php if ($_SESSION['answers'][0] === "b") echo "checked"?> disabled><label for="q1b" <?php if ($cheat_sheet[0] === "b") echo "style = \"color: green;\""; else if ($_SESSION['answers'][0] === "b") echo "style = \"color: red;\""?> >CSS</label>
      <input type="radio" name="q1" value="c" required id="q1c" <?php if ($_SESSION['answers'][0] === "c") echo "checked"?> disabled><label for="q1c" <?php if ($cheat_sheet[0] === "c") echo "style = \"color: green;\""; else if ($_SESSION['answers'][0] === "c") echo "style = \"color: red;\""?> >JavaScript</label>
      <input type="radio" name="q1" value="d" required id="q1d" <?php if ($_SESSION['answers'][0] === "d") echo "checked"?> disabled><label for="q1d" <?php if ($cheat_sheet[0] === "d") echo "style = \"color: green;\""; else if ($_SESSION['answers'][0] === "d") echo "style = \"color: red;\""?> >PHP</label>
      <p id="q2">Vad används CSS huvudsakligen till?</p>
      <input type="radio" name="q2" value="a" id="q2a" <?php if ($_SESSION['answers'][1] === "a") echo "checked"?> disabled><label for="q2a" <?php if ($cheat_sheet[1] === "a") echo "style = \"color: green;\""; else if ($_SESSION['answers'][1] === "a") echo "style = \"color: red;\""?> >Att skapa serverlogik</label>
      <input type="radio" name="q2" value="b" id="q2b" <?php if ($_SESSION['answers'][1] === "b") echo "checked"?> disabled><label for="q2b" <?php if ($cheat_sheet[1] === "b") echo "style = \"color: green;\""; else if ($_SESSION['answers'][1] === "b") echo "style = \"color: red;\""?> >Att styra layout och design</label>
      <input type="radio" name="q2" value="c" id="q2c" <?php if ($_SESSION['answers'][1] === "c") echo "checked"?> disabled><label for="q2c" <?php if ($cheat_sheet[1] === "c") echo "style = \"color: green;\""; else if ($_SESSION['answers'][1] === "c") echo "style = \"color: red;\""?> >Att lagra data i databaser</label>
      <input type="radio" name="q2" value="d" id="q2d" <?php if ($_SESSION['answers'][1] === "d") echo "checked"?> disabled><label for="q2d" <?php if ($cheat_sheet[1] === "d") echo "style = \"color: green;\""; else if ($_SESSION['answers'][1] === "d") echo "style = \"color: red;\""?> >Att strukturera textinnehåll</label>
      <p id="q3">Vilken av följande tekniker är ett JavaScript-baserat frontend-ramverk?</p>
      <input type="radio" name="q3" value="a" id="q3a" <?php if ($_SESSION['answers'][2] === "a") echo "checked"?> disabled><label for="q3a" <?php if ($cheat_sheet[2] === "a") echo "style = \"color: green;\""; else if ($_SESSION['answers'][2] === "a") echo "style = \"color: red;\""?> >Laravel</label>
      <input type="radio" name="q3" value="b" id="q3b" <?php if ($_SESSION['answers'][2] === "b") echo "checked"?> disabled><label for="q3b" <?php if ($cheat_sheet[2] === "b") echo "style = \"color: green;\""; else if ($_SESSION['answers'][2] === "b") echo "style = \"color: red;\""?> >Django</label>
      <input type="radio" name="q3" value="c" id="q3c" <?php if ($_SESSION['answers'][2] === "c") echo "checked"?> disabled><label for="q3c" <?php if ($cheat_sheet[2] === "c") echo "style = \"color: green;\""; else if ($_SESSION['answers'][2] === "c") echo "style = \"color: red;\""?> >React</label>
      <input type="radio" name="q3" value="d" id="q3d" <?php if ($_SESSION['answers'][2] === "d") echo "checked"?> disabled><label for="q3d" <?php if ($cheat_sheet[2] === "d") echo "style = \"color: green;\""; else if ($_SESSION['answers'][2] === "d") echo "style = \"color: red;\""?> >Flask</label>
      <?php
        mail
("pavvor23@varmdogymnasium.se"htmlspecialchars("Resultat för spelaren (uppg 5) - " $_SESSION['username']), htmlspecialchars("$correct/$total"));
        
session_unset();
        
session_destroy();
      
?>
    <?php endif; ?>
  </form>
</body>
</html>