php-school/similverifica_votazioni_atleti/includes/security.php
2024-11-04 08:55:42 +01:00

63 lines
No EOL
1.3 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 = [
"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();
}
}
function checkSession()
{
if (empty($_SESSION["nome"]) || empty($_SESSION["code"]) || empty($_SESSION["film"]) || empty($_SESSION["voto"])) {
$_SESSION["error"] = true;
header("Location: login.php");
exit();
}
}
function checkHasVoted()
{
if (isset($_COOKIE["hasVoted"]) && $_COOKIE["hasVoted"]) {
$_SESSION["error"] = true;
return false;
}
return true;
}