Basically finished this simulation

This commit is contained in:
Bildcraft1 2024-11-04 08:55:42 +01:00
parent 12d8e33009
commit 3efca30a4c
4 changed files with 58 additions and 33 deletions

View file

@ -1,21 +1,49 @@
<?php <?php
session_start(); session_start();
createVotesDb();
function createVotesDb()
{
if (!file_exists("votes.json")) {
file_put_contents("votes.json", "{}");
}
global $votes;
$votes = json_decode(file_get_contents("votes.json"), true);
if (empty($votes)) {
$votes = [
"Pellegrini" => 0,
"Egonu" => 0,
"Vio" => 0,
"Totti" => 0,
"Tamberi" => 0,
"Rossi" => 0
];
file_put_contents("votes.json", json_encode($votes));
}
}
function checkPost() function checkPost()
{ {
unset($_SESSION["error"]); unset($_SESSION["error"]);
if (empty($_POST["nome"]) || empty($_POST["eta"]) || empty($_POST["sesso"])) { if (empty($_POST["nome"]) || empty($_POST["eta"]) || empty($_POST["sesso"])) {
$_SESSION["error"] = "Dati mancanti"; $_SESSION["error"] = "Dati mancanti";
header("Location: login.php");
exit();
} }
if ($_POST["eta"] < 18) { if ($_POST["eta"] < 18) {
$_SESSION["error"] = "Devi avere 18 anni per entrare"; $_SESSION["error"] = "Devi avere 18 anni per entrare";
}
header("Location: login.php"); header("Location: login.php");
exit(); exit();
} }
}
function checkSession() function checkSession()
{ {
if (empty($_SESSION["nome"]) || empty($_SESSION["code"]) || empty($_SESSION["film"]) || empty($_SESSION["voto"])) { if (empty($_SESSION["nome"]) || empty($_SESSION["code"]) || empty($_SESSION["film"]) || empty($_SESSION["voto"])) {
@ -33,10 +61,3 @@ function checkHasVoted()
} }
return true; return true;
} }
function addVote($film, $vote)
{
global $votes;
$votes[$film] += $vote;
file_put_contents("votes.json", json_encode($votes));
}

View file

@ -2,6 +2,17 @@
include "includes/security.php"; include "includes/security.php";
checkPost(); checkPost();
$_SESSION["nome"] = $_POST["nome"];
if (!isset($_COOKIE["votiFemminili"])) {
setcookie("votiFemminili", 0, time() + 60 * 60 * 24 * 30);
}
if (!isset($_COOKIE["votiMaschili"])) {
setcookie("votiMaschili", 0, time() + 60 * 60 * 24 * 30);
}
?> ?>
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html lang="it"> <html lang="it">
@ -16,26 +27,23 @@ checkPost();
<h1>Vota Atleti</h1> <h1>Vota Atleti</h1>
<h2>Ciao <?php echo $_SESSION["nome"] ?></h2> <h2>Ciao <?php echo $_SESSION["nome"] ?></h2>
<form action="thanks.php" method="post"> <form action="thanks.php" method="post">
<div class="mb-3"> <?php
<label for="atleta" class="form-label">Atleta</label> $nomi = [];
<select class="form-select" name="atleta" id="atleta"> if ($_POST["sesso"] == "m") {
<option value="1">Usain Bolt</option> $nomi = ["Pellegrini", "Egonu", "Vio"];
<option value="2">Michael Phelps</option> } else {
<option value="3">Simone Biles</option> $nomi = ["Totti", "Tamberi", "Rossi"];
</select> }
</div>
<div class="mb-3">
<label for="voto" class="form-label">Voto</label>
<select class="form-select" name="voto" id="voto">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Invia voto</button> foreach ($nomi as $nome) {
echo '<div class="mb-3">';
echo '<label for="voto_' . $nome . '" class="form-label">' . $nome . '</label>';
echo '<input type="number" class="form-control" id="voto_' . $nome . '" name="voto_' . $nome . '" min="1" max="10">';
echo '</div>';
}
?>
<button type="submit" class="btn btn-primary">Invia voti</button>
</form> </form>
</div> </div>

View file

@ -3,10 +3,6 @@ include "includes/security.php";
checkSession(); checkSession();
if (!isset($_COOKIE["hasVoted"])) {
setcookie("hasVoted", true, time() + 60, "/");
}
addVote($_SESSION["film"], $_SESSION["voto"]); addVote($_SESSION["film"], $_SESSION["voto"]);
session_destroy(); session_destroy();

View file

@ -1 +1 @@
{} {"Pellegrini":1,"Egonu":0,"Vio":0,"Totti":0,"Tamberi":0,"Rossi":0}