I'm working in a PHP project where I have 7 types of users, so I have to anticipate 7 $_SESSION variables for every type of user. Is there a way I can avoid doing this in every PHP file?
<?php
if (isset($_GET['logout'])) {
session_destroy();
header('Location: .');
}
if (isset($_SESSION['admin'])) {
header('Location: views/admin/');
}
if (isset($_SESSION['info'])) {
header('Location: views/informatica/');
}
if (isset($_SESSION['subA'])) {
header('Location: views/subA/');
}
if (isset($_SESSION['subB'])) {
header('Location: views/subB/');
}
if (isset($_SESSION['oficina'])) {
header('Location: views/oficina/');
}
if (isset($_SESSION['compras'])) {
header('Location: views/compras/');
}
if (isset($_SESSION['auditoria'])) {
header('Location: views/auditoria/');
}
CodePudding user response:
if (isset($_GET['logout'])) {
session_destroy();
header('Location: /');
exit();
}
$rolesLocations = [
'admin' => '/views/admin',
'info' => '/views/informatica',
'subA' => '/views/subA',
'subB' => '/views/subB',
'oficina' => '/views/oficina',
'compras' => '/views/compras',
'auditoria' => '/views/auditoria',
];
foreach ($rolesLocations as $role => $location) {
if (isset($_SESSION[$role])) {
header('Location: ' . $location);
}
}
CodePudding user response:
Instead of separate variables, use one variable $_SESSION['usertype']
.
if (isset($_SESSION['usertype'])) {
header("Location: views/{$_SESSION['usertype']}/");
}