Show sourcecode
The following files exists in this folder. Click to view.
Projekt/
cm.html
formPostTest/
index.html
javascript.html
ovning0.html
ovning01.html
ovning02.html
ovning03.html
ovning04.html
ovning06.html
ovning07.html
ovning08.html
ovning09.css
ovning09ap.html
ovning09app.html
ovning09ba.html
ovning10.html
ovningjs02.html
piano.html
quizeovning.php
spel.html
test.html
quizeovning.php
156 lines UTF-8 Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
<?php
$password = 'axelaxel';
$questions = [
['Vad är Frankrikes huvudstad?', 'Paris', 'Nice', 'Val Thorens', 'Frankfurt'],
['Vad heter min brorsa?', 'Alex', 'Vincent', 'Karl', 'Sixten'],
['Vad är roligast?', 'Webb', 'Svenska', 'Fysik', 'Mina egna projekt'],
];
$correct = ['Paris', 'Vincent', 'Webb'];
$message = '';
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$answers = [$_POST['answer1'] ?? '', $_POST['answer2'] ?? '', $_POST['answer3'] ?? ''];
$index = (int) ($_POST['index'] ?? 0);
$index = max(0, min(2, $index));
$token = hash_hmac('sha256', 'quiz', $password);
$access = isset($_POST['access']) && is_string($_POST['access']) ? $_POST['access'] : '';
$loggedIn = hash_equals($token, $access);
$result = null;
function h($text)
{
return htmlspecialchars((string) $text, ENT_QUOTES, 'UTF-8');
}
if (isset($_POST['logout'])) {
$loggedIn = false;
$access = '';
}
if (isset($_POST['password'])) {
if ($_POST['password'] === $password) {
$loggedIn = true;
$access = $token;
} else {
$message = 'Fel lösenord.';
}
}
if (isset($_POST['restart']) && $loggedIn) {
$index = 0;
$name = '';
$email = '';
$answers = ['', '', ''];
}
if (isset($_POST['answer']) && $loggedIn) {
$answer = $_POST['answer'];
if ($name === '') {
$message = 'Skriv ditt namn.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$message = 'Skriv en giltig mejladress.';
} elseif (!in_array($answer, array_slice($questions[$index], 1), true)) {
$message = 'Välj ett svar.';
} else {
$answers[$index] = $answer;
if ($index < 2) {
$index++;
} else {
$score = 0;
foreach ($answers as $i => $answer) {
if ($answer === $correct[$i]) {
$score++;
}
}
$mail = mail($email, 'Quizresultat', "Spelare: $name\nAntal rätt: $score av 3", 'From: axelquiz@axel.se');
$result = [
'name' => $name,
'answers' => $answers,
'score' => $score,
'mail' => $mail,
];
}
}
}
?>
<!doctype html>
<html lang="sv">
<head>
<meta charset="utf-8">
<title>Frågetävling</title>
<style>
body { max-width: 600px; margin: 30px auto; font: 16px Arial; }
.correct { color: green; font-weight: bold; }
.wrong { color: red; font-weight: bold; }
</style>
</head>
<body>
<h1>Frågetävling</h1>
<?php if ($message): ?><p class="error"><?= h($message) ?></p><?php endif; ?>
<?php if (!$loggedIn): ?>
<form method="post">
<label>Lösenord <input type="password" name="password" required></label>
<button>Öppna</button>
</form>
<?php elseif ($result): ?>
<h2>Resultat</h2>
<p><?= h($result['name']) ?> fick <?= $result['score'] ?> av 3 rätt.</p>
<?php foreach ($questions as $i => $question): ?>
<p><b><?= $i + 1 ?>. <?= h($question[0]) ?></b></p>
<?php foreach (array_slice($question, 1) as $option): ?>
<?php
$class = $option === $correct[$i]
? 'correct'
: ($option === $result['answers'][$i] ? 'wrong' : '');
?>
<div class="<?= $class ?>"><?= h($option) ?></div>
<?php endforeach; ?>
<?php endforeach; ?>
<p><?= $result['mail'] ? 'Resultatet har skickats med e-post.' : 'E-post kunde inte skickas.' ?></p>
<form method="post" style="display:inline">
<input type="hidden" name="access" value="<?= h($access) ?>">
<button name="restart">Gör om</button>
</form>
<form method="post" style="display:inline">
<input type="hidden" name="access" value="<?= h($access) ?>">
<button name="logout">Logga ut</button>
</form>
<?php else: ?>
<p>Fråga <?= $index + 1 ?> av 3</p>
<form method="post">
<input type="hidden" name="access" value="<?= h($access) ?>">
<input type="hidden" name="index" value="<?= $index ?>">
<?php if ($index > 0): ?><input type="hidden" name="name" value="<?= h($name) ?>"><?php endif; ?>
<?php if ($index > 0): ?><input type="hidden" name="email" value="<?= h($email) ?>"><?php endif; ?>
<input type="hidden" name="answer1" value="<?= h($answers[0]) ?>">
<input type="hidden" name="answer2" value="<?= h($answers[1]) ?>">
<input type="hidden" name="answer3" value="<?= h($answers[2]) ?>">
<?php if ($index === 0): ?>
<label>Namn <input type="text" name="name" value="<?= h($name) ?>" required></label>
<label>Mejl <input type="email" name="email" value="<?= h($email) ?>" required></label>
<?php endif; ?>
<p><b><?= h($questions[$index][0]) ?></b></p>
<?php foreach (array_slice($questions[$index], 1) as $option): ?>
<label><input type="radio" name="answer" value="<?= h($option) ?>" required> <?= h($option) ?></label>
<?php endforeach; ?>
<button><?= $index === 2 ? 'Skicka in' : 'Nästa fråga' ?></button>
</form>
<?php endif; ?>
</body>
</html>