Webbserverprogrammering 1

Show sourcecode

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

webbsrvprg/exercises/

InlämningsFormulär.php
include1.php
include2.php
includesid1.php
includesid2.php
includesid3.php
phpformulär1.php
phpformulär2.php
phpslump6.php
sidslut.php
sidstart.php
test.php
övning1.php
övning2.php
övning3.php
övning4.php
övning5.php
övningfunk1.php
övningfunk2.php
övningfunk3.php
övningfunk4.php
övningfunk5.php
övningfunk6.php
övningtest.php

test.php

206 lines UTF-8 Windows (CRLF)
<?php
session_start
();

if (isset(
$_POST["logga_in"])) {
    if ((
$_POST["losenord"] ?? "") === "Wiggo") {
        
session_regenerate_id(true);
        
$_SESSION["inloggad"] = true;
    } else {
        
$fel = true;
    }
}

if (empty(
$_SESSION["inloggad"])) {
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Lösenord</title>
    <style>
        body {
            text-align: center;
            font-family: Arial, sans-serif;
        }
    </style>
</head>
<body>

<h2>Ange lösenord</h2>

<?php if (isset($fel)) { ?>
    <p>Fel lösenord!</p>
<?php } ?>

<form method="post">
    <input type="password" name="losenord" placeholder="Lösenord" required>
    <input type="submit" name="logga_in" value="Logga in">
</form>

</body>
</html>
<?php
    
exit;
}

$steg = (int)($_POST["steg"] ?? 1);

$namn = $_POST["namn"] ?? "";
$f1 = $_POST["f1"] ?? "";
$f2 = $_POST["f2"] ?? "";
$f3 = $_POST["f3"] ?? "";

$mottagare = "alewel25@varmdogymnasium.se";

if (isset(
$_POST["nasta"])) {
    
$steg++;
}

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

    if (
$f1 == "Sverige") $poang++;
    if (
$f2 == "Evelina") $poang++;
    if (
$f3 == "Azog the defiler") $poang++;

    echo 
"<h2>Resultat</h2>";
    echo 
"Namn: " . htmlspecialchars($namn, ENT_QUOTES, "UTF-8") . "<br>";
    echo 
"Antal rätt: " . $poang . " av 3";

    
$rubrik = "Resultat från frågesporten";

    
$meddelande = "Hej!\n\n";
    
$meddelande .= "Namn: " . $namn . "\n";
    
$meddelande .= "Antal rätt: " . $poang . " av 3\n";

    
$mejlhuvud = "From: alewel25@varmdogymnasium.se\r\n";
    
$mejlhuvud .= "Content-Type: text/plain; charset=UTF-8\r\n";

    if (
filter_var($mottagare, FILTER_VALIDATE_EMAIL)) {
        if (
mail($mottagare, $rubrik, $meddelande, $mejlhuvud)) {
            echo 
"<p>PHP har accepterat mejlet för utskick.</p>";
        } else {
            echo 
"<p>Mejlet kunde inte skickas. Kontrollera Laragons mejlinställningar.</p>";
        }
    } else {
        echo 
"<p>Kontrollera mottagarens mejladress i koden.</p>";
    }

} else {
?>

<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Frågesport</title>

    <style>
        body {
            text-align: center;
            font-family: Arial, sans-serif;
        }

        form {
            width: 300px;
            margin: auto;
        }

        .alternativ {
            text-align: left;
            margin-left: 30px;
        }

        .alternativ label {
            display: block;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>

<form method="post">

<?php if ($steg == 1) { ?>

    <h2>Vad heter du?</h2>
    <input type="text" name="namn" required>

<?php } ?>

<?php if ($steg >= 2) { ?>
    <input type="hidden" name="namn"
        value="<?= htmlspecialchars($namn, ENT_QUOTES, 'UTF-8') ?>">
<?php } ?>

<?php if ($steg == 2) { ?>

    <h2>Vad bor Alexander?</h2>

    <div class="alternativ">
        <label><input type="radio" name="f1" value="Sverige" required> Sverige</label>
        <label><input type="radio" name="f1" value="USA"> USA</label>
        <label><input type="radio" name="f1" value="Tyskland"> Tyskland</label>
        <label><input type="radio" name="f1" value="Norge"> Norge</label>
    </div>

<?php } ?>

<?php if ($steg >= 3) { ?>
    <input type="hidden" name="f1"
        value="<?= htmlspecialchars($f1, ENT_QUOTES, 'UTF-8') ?>">
<?php } ?>

<?php if ($steg == 3) { ?>

    <h2>Vad heter Alexanders syster?</h2>

    <div class="alternativ">
        <label><input type="radio" name="f2" value="Alice" required> Alice</label>
        <label><input type="radio" name="f2" value="Evelina"> Evelina</label>
        <label><input type="radio" name="f2" value="Lisa"> Lisa</label>
        <label><input type="radio" name="f2" value="Habibi"> Habibi</label>
    </div>

<?php } ?>

<?php if ($steg >= 4) { ?>
    <input type="hidden" name="f2"
        value="<?= htmlspecialchars($f2, ENT_QUOTES, 'UTF-8') ?>">
<?php } ?>

<?php if ($steg == 4) { ?>

    <h2>Vad är Wiggo?</h2>

    <div class="alternativ">
        <label><input type="radio" name="f3" value="En goblin" required> En goblin</label>
        <label><input type="radio" name="f3" value="Ett troll"> Ett troll</label>
        <label><input type="radio" name="f3" value="Ork"> Ork</label>
        <label><input type="radio" name="f3" value="Azog the defiler"> Azog the defiler</label>
    </div>

<?php } ?>

<?php if ($steg < 4) { ?>

    <input type="hidden" name="steg" value="<?= $steg ?>">
    <br><br>
    <input type="submit" name="nasta" value="Nästa">

<?php } else { ?>

    <input type="hidden" name="steg" value="4">
    <br><br>
    <input type="submit" name="skicka" value="Skicka in">

<?php } ?>

</form>

</body>
</html>

<?php
}
?>