Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/repetition/
hemligheter.php
repetition.php
repetition1.php
repetition2.php
repetition3.php
repetition3_medel.php
repetition4.php
repetition5.php
repetition6.php
repetition7.php
repetition8.php
repetition5.php
59 lines UTF-8 Windows (CRLF)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Repetition</title>
</head>
<body>
<?php
session_start();
if (!isset($_SESSION["namn"]) && isset($_GET["namn"])) {
if (htmlspecialchars($_GET["namn"]) != "") {
$_SESSION["namn"] = htmlspecialchars($_GET["namn"]);
$_SESSION["tries"] = 0;
}
}
if (isset($_SESSION["tries"])) {
$_SESSION["tries"] = (int)$_SESSION["tries"] + 1;
}
$rand = rand(1,10);
if (isset($_GET["number"]) && isset($_SESSION["namn"])) {
echo "<p>Hej ".$_SESSION["namn"]."! <br>";
if (htmlspecialchars($_GET["number"]) == $rand) {
echo "Du gissade rätt! Det tog ".$_SESSION["tries"]." försök.</p>";
session_destroy();
} else {
echo "Fel, du har nu gissat ".$_SESSION["tries"]." gånger.</p>";
}
}
?>
<form method="GET" action="">
<?php if (!isset($_SESSION["namn"])) {
echo '<input type="text" name="namn" placeholder="Namn"><br>';
}
if (isset($_GET["number"])) {
if (htmlspecialchars($_GET["number"]) != $rand) {
echo '<input type="number" name="number" placeholder="Nummer">';
}
} else {
echo '<input type="number" name="number" placeholder="Nummer">';
}
?>
<br>
<input type="submit" value="Testa">
</form>
</body>
</html>