php-school/similverifica_votazioni_atleti/includes/security.php
2024-11-06 21:12:42 +01:00

81 lines
No EOL
1.8 KiB
PHP

<?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 = [
"voto_Pellegrini" => 0,
"voto_Egonu" => 0,
"voto_Vio" => 0,
"voto_Totti" => 0,
"voto_Tamberi" => 0,
"voto_Rossi" => 0
];
file_put_contents("votes.json", json_encode($votes));
}
}
function addVote($votes, $sex)
{
// Add the vote to the counter
if (strcmp($sex, 'M') == 0) {
setcookie("votiMaschili", $_COOKIE['votiMaschili'] + 1, time() + 60 * 60 * 24 * 30);
} else {
setcookie("votiFemminili", $_COOKIE['votiFemminili'] + 1, time() + 60 * 60 * 24 * 30);
}
foreach ($votes as $key => $value) {
global $votes;
$votes[$key] += $value;
}
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();
}
}
function checkSession()
{
if (empty($_SESSION["nome"]) || empty($_SESSION["sesso"])) {
$_SESSION["error"] = "Errore interno (Sessione non trovata)";
header("Location: login.php");
exit();
}
}
function checkHasVoted()
{
if (isset($_COOKIE["hasVoted"]) && $_COOKIE["hasVoted"]) {
$_SESSION["error"] = true;
return false;
}
return true;
}