Files
Progetto-PHP/PHP/includes/auth.php
T
Emanuele Slusarz ec30fb40b9 first commit
2026-08-22 11:15:29 +02:00

47 lines
1.1 KiB
PHP
Executable File

<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
function requireLogin(): void
{
if (empty($_SESSION["idUtente"])) {
$cartellaScript = basename(dirname($_SERVER["SCRIPT_NAME"] ?? ""));
$login = $cartellaScript === "PHP" ? "index.php" : "../index.php";
header("Location: " . $login . "?errore=accesso");
exit;
}
}
function requireRole(string $ruolo): void
{
requireLogin();
if (($_SESSION["ruolo"] ?? null) !== $ruolo) {
header("Location: ../dashboard.php?errore=autorizzazione");
exit;
}
}
function dashboardForRole(?string $ruolo): string
{
$destinazioni = [
"cliente" => "cliente/dashboard.php",
"personale" => "personale/dashboard.php",
"proprietario" => "proprietario/dashboard.php",
];
return $destinazioni[$ruolo] ?? "index.php";
}
function e(mixed $valore): string
{
return htmlspecialchars((string) $valore, ENT_QUOTES, "UTF-8");
}
function postMethod(): bool
{
return ($_SERVER["REQUEST_METHOD"] ?? "GET") === "POST";
}