Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/ovningar/

index.php
ovn1_gr.php
ovn2_gr.php
ovn_formular.php
ovn_formularDestination.php
ovn_funky.php
ovn_gr.php
ovn_inkl.php
ovn_inkl2,1.php
ovn_inkl2,2.php
ovn_inkl2,3.php
ovn_inkl2slut.php
ovn_inkl2start.php
ovn_inklHejsvejs.php
uppg_quiz.php

uppg_quiz.php

115 lines UTF-8 Windows (CRLF)
<style>
    body {
        text-align: center;
        font-family: Arial;
    }
    form {
        width: 400px;
        margin: auto;
    }
    .alternativ {
        text-align: left;
        margin-left: 30px;
    }
    .alternativ label {
        display: block;
        margin-bottom: 10px;
    }
</style>

<?php
$steg 
= isset($_POST["steg"]) ? (int)$_POST["steg"] : 1;
$namn = $_POST["namn"] ?? "";
$epost = $_POST["epost"] ?? "";
$f1 = $_POST["f1"] ?? "";
$f2 = $_POST["f2"] ?? "";
$f3 = $_POST["f3"] ?? "";

if (isset(
$_POST["skicka"])) {
    
$poang = 0;

    if (
$f1 == "Why?") $poang++;
    if (
$f2 == "Depends...") $poang++;
    if (
$f3 == "No") $poang++;

    echo 
"<h2>Power resides where men believe it resides. It's a trick. A shadow on the wall.</h2>";
    echo 
"My liege " . htmlspecialchars($namn) . ". <br>";
    echo 
"You had " . $poang . " out of 3 correct.<br>";

    if (!empty(
$epost)) {
        
$till = $epost;
        
$ämne = "Resultat fran fragetavling";
        
$meddelande = "My liege $namn,\n\nHere is your result:\nYou had $poang out of 3 correct.\n\nValar Dohaeris! Valar Morghulis!";
        
$headers = "From: $epost \nReply-To: wigjor25@varmdogymnasium.se";

        if (
mail($till, $ämne, $meddelande, $headers)) {
            echo 
"<p style='color: green;'>A raven has been sent to deliver the results to your cote.</p>";
        } else {
            echo 
"<p style='color: red;'>We have been sent poor weather. The ravens can't fly tonight.</p>";
        }
    } else {
        echo 
"<p style='color: red;'>No raven can fly without an address, my liege.</p>";
    }

} else {
?>

<form method="post">

    <input type="hidden" name="namn" value="<?php echo htmlspecialchars($namn); ?>">
    <input type="hidden" name="epost" value="<?php echo htmlspecialchars($epost); ?>">
    <input type="hidden" name="f1" value="<?php echo htmlspecialchars($f1); ?>">
    <input type="hidden" name="f2" value="<?php echo htmlspecialchars($f2); ?>">

    <?php if ($steg == 1) { ?>
        <h2>Who are you, my liege?</h2>
        <p>Please, enter your true name.</p>
        <input type="text" name="namn" required value="<?php echo htmlspecialchars($namn); ?>">
        <p>Where shall your answers be sent? (e-post)</p>
        <input type="email" name="epost" required value="<?php echo htmlspecialchars($epost); ?>">
        <br><br>
        <input type="hidden" name="steg" value="2">
        <input type="submit" name="nasta" value="Nästa">

    <?php } elseif ($steg == 2) { ?>
        <h2>Power is a curious thing, my liege. Are you fond of riddles?</h2>
        <div class="alternativ">
            <label><input type="radio" name="f1" value="Why?" required> Why? Am I about to hear one?</label>
            <label><input type="radio" name="f1" value="No"> Not at all, dear enquirer.</label>
            <label><input type="radio" name="f1" value="Yes"> Great riddles build great minds.</label>
            <label><input type="radio" name="f1" value="Norge"> Norge</label>
        </div>
        <br>
        <input type="hidden" name="steg" value="3">
        <input type="submit" name="nasta" value="Nästa">

    <?php } elseif ($steg == 3) { ?>
        <h2>Three great men sit in a room, a king, a priest and a rich man. Between them stands a common sellsword. Each great man bids the sellsword kill the other two. Who lives? Who dies?</h2>
        <div class="alternativ">
            <label><input type="radio" name="f2" value="King" required> The king lives. Though sellswords are not technically sworn to the crown, their minds always will be.</label>
            <label><input type="radio" name="f2" value="Priest"> Clearly the priest, he is favoured by the gods. No man shall slay him.</label>
            <label><input type="radio" name="f2" value="Rich man"> A Lannister always pays his debts. Gold is what drives peoples' brains. He shall surely survive.</label>
            <label><input type="radio" name="f2" value="Depends..."> Depends on the sellsword. Though he has no crown nor gold, nor favour with the gods, he has a sword! The power of life and death.</label>
        </div>
        <br>
        <input type="hidden" name="steg" value="4">
        <input type="submit" name="nasta" value="Nästa">

    <?php } elseif ($steg == 4) { ?>
        <h2>Why do we pretend kings hold all the power? When Ned Stark lost his head, who was truly responsible: Joffrey, the executioner, or something else?</h2>
        <div class="alternativ">
            <label><input type="radio" name="f3" value="King" required> Joffrey. That bastard king is not only cruel, not only stupid. He is both evil and dense, that boy.</label>
            <label><input type="radio" name="f3" value="Executioner"> The executioner wielded Ice. He acts as the cellsword from before and made the final judgement.</label>
            <label><input type="radio" name="f3" value="No"> I have decided I don't like riddles.</label>
            <label><input type="radio" name="f3" value="Other"> It was Littlefinger. He betrayed lord Stark, hand of the king and successor to the Iron Throne.</label>
        </div>
        <br>
        <input type="hidden" name="steg" value="4">
        <input type="submit" name="skicka" value="Skicka in">
    <?php } ?>

</form>

<?php
}
?>