Basically finished this simulation
This commit is contained in:
parent
12d8e33009
commit
3efca30a4c
4 changed files with 58 additions and 33 deletions
|
@ -1,19 +1,47 @@
|
|||
<?php
|
||||
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()
|
||||
{
|
||||
unset($_SESSION["error"]);
|
||||
|
||||
if (empty($_POST["nome"]) || empty($_POST["eta"]) || empty($_POST["sesso"])) {
|
||||
$_SESSION["error"] = "Dati mancanti";
|
||||
header("Location: login.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
if ($_POST["eta"] < 18) {
|
||||
$_SESSION["error"] = "Devi avere 18 anni per entrare";
|
||||
header("Location: login.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
header("Location: login.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
function checkSession()
|
||||
|
@ -33,10 +61,3 @@ function checkHasVoted()
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function addVote($film, $vote)
|
||||
{
|
||||
global $votes;
|
||||
$votes[$film] += $vote;
|
||||
file_put_contents("votes.json", json_encode($votes));
|
||||
}
|
|
@ -2,6 +2,17 @@
|
|||
include "includes/security.php";
|
||||
|
||||
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>
|
||||
<html lang="it">
|
||||
|
@ -16,26 +27,23 @@ checkPost();
|
|||
<h1>Vota Atleti</h1>
|
||||
<h2>Ciao <?php echo $_SESSION["nome"] ?></h2>
|
||||
<form action="thanks.php" method="post">
|
||||
<div class="mb-3">
|
||||
<label for="atleta" class="form-label">Atleta</label>
|
||||
<select class="form-select" name="atleta" id="atleta">
|
||||
<option value="1">Usain Bolt</option>
|
||||
<option value="2">Michael Phelps</option>
|
||||
<option value="3">Simone Biles</option>
|
||||
</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>
|
||||
<?php
|
||||
$nomi = [];
|
||||
if ($_POST["sesso"] == "m") {
|
||||
$nomi = ["Pellegrini", "Egonu", "Vio"];
|
||||
} else {
|
||||
$nomi = ["Totti", "Tamberi", "Rossi"];
|
||||
}
|
||||
|
||||
<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>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -3,10 +3,6 @@ include "includes/security.php";
|
|||
|
||||
checkSession();
|
||||
|
||||
if (!isset($_COOKIE["hasVoted"])) {
|
||||
setcookie("hasVoted", true, time() + 60, "/");
|
||||
}
|
||||
|
||||
addVote($_SESSION["film"], $_SESSION["voto"]);
|
||||
|
||||
session_destroy();
|
||||
|
|
|
@ -1 +1 @@
|
|||
{}
|
||||
{"Pellegrini":1,"Egonu":0,"Vio":0,"Totti":0,"Tamberi":0,"Rossi":0}
|
Loading…
Add table
Reference in a new issue