How to avoid using a lot of issets?

GLPG35

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/');
}
Peter01
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);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related