Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/

cookies.php
formular.php
fragetest3.php
funktioner.php
grunder.php
include/
klasser.php
ovn_cookies1.php
ovn_cookies2.php
ovn_form1.php
ovn_form2.php
ovn_form3.php
ovn_form4.php
ovn_form6.php
ovn_funk1.php
ovn_funk2.php
ovn_funk3.php
ovn_funk4.php
ovn_funk5.php
ovn_grn1.php
ovn_grn2.php
ovn_grn3a.php
ovn_grn3b.php
ovn_include1.php
ovn_klasser1.php
ovn_string1.php
ovn_string2.php
ovn_string3.php
ovn_stringsvar1.php
ovn_stringsvar2.php
ovn_stringsvar3.php
sida1.php
string.php

fragetest3.php

93 lines UTF-8 Windows (CRLF)
<!DOCTYPE html>
<html>
<body>

<h1>Peters Quiz</h1>

<form method="post" action="">
    <?php
    session_start
();
    if (!isset(
$_SESSION['name'])) {
        if (
$_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["namn"])) {
            
$_SESSION['name'] = $_POST["namn"];
        } else {
            
?>
            Namn: <input type="text" name="namn" required><br>
            <input type="submit" value="Starta Quiz">
            <?php
            
exit;
        }
    }
    
?>

    <?php
    
if (!isset($_POST['currentQuestion'])) {
        
$currentQuestion 1;
        
$_SESSION['score'] = 0;
    } else {
        
$currentQuestion $_POST['currentQuestion'];
    }

    if (
$_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["answer"])) {
        
$correctAnswers = [
            
"bench" => "q1a2",
            
"matkort" => "q2a4",
            
"SD" => "q3a2"
        
];

        if (
$currentQuestion == && $_POST["answer"] == $correctAnswers["bench"]) {
            
$_SESSION['score']++;
        } elseif (
$currentQuestion == && $_POST["answer"] == $correctAnswers["matkort"]) {
            
$_SESSION['score']++;
        } elseif (
$currentQuestion == && $_POST["answer"] == $correctAnswers["SD"]) {
            
$_SESSION['score']++;
        }

        
$currentQuestion++;
    }

    if (
$currentQuestion == 1) {
        
?>
        <h3>Fråga 1: Vad maxar jag i bänkpress?</h3>
        <input type="radio" name="answer" value="q1a1"><label for="q1a1">105 kg</label><br>
        <input type="radio" name="answer" value="q1a2"><label for="q1a2">97,5 kg</label><br>
        <input type="radio" name="answer" value="q1a3"><label for="q1a3">90 kg</label><br>
        <input type="radio" name="answer" value="q1a4"><label for="q1a4">80 kg &#128514;</label><br>
        
        <input type="hidden" name="currentQuestion" value="1">
        
        <?php
    
} elseif ($currentQuestion == 2) {
        
?>
        <h3>Fråga 2: Vilket är det bästa stället på matkortet, om man prioriterar kvalitet?</h3>
        <input type="radio" name="answer" value="q2a1"><label for="q2a1">Hemköp</label><br>
        <input type="radio" name="answer" value="q2a2"><label for="q2a2">Bastard Burgers</label><br>
        <input type="radio" name="answer" value="q2a3"><label for="q2a3">Subway</label><br>
        <input type="radio" name="answer" value="q2a4"><label for="q2a4">Bamses Pizzeria</label><br>
        
        <input type="hidden" name="currentQuestion" value="2">
        
        <?php
    
} elseif ($currentQuestion == 3) {
        
?>
        <h3>Fråga 3: Vem är partiledare för Sverigedemokraterna?</h3>
        <input type="radio" name="answer" value="q3a1"><label for="q3a1">Leif GW Persson</label><br>
        <input type="radio" name="answer" value="q3a2"><label for="q3a2">Jimmie Åkesson</label><br>
        <input type="radio" name="answer" value="q3a3"><label for="q3a3">Ronald Reagan</label><br>
        <input type="radio" name="answer" value="q3a4"><label for="q3a4">Carl Bildt</label><br><br>
        
        <input type="hidden" name="currentQuestion" value="3">
        
        <?php
    
} elseif ($currentQuestion == 4) {
        echo 
"<h2>" $_SESSION['name'] . ", du fick " $_SESSION['score'] . " av 3 rätt!</h2>";
        
session_destroy();
    }
    
?>

    <input type="submit" value="Skicka">
</form>

</body>
</html>