Skip to content
Advertisement

Header issue does not load the right one

Hi so i have couple of headers that have different menus for different users and my code is

if (isset($_SESSION["role"])== "engineer" or isset($_SESSION["role"])!="admin") {
    include 'engineerheader.php';
  }else if (isset($_SESSION["role"])!= "engineer" or isset($_SESSION["role"])=="admin") {
    include 'adminheader.php';

 } else{
    include 'workerheader.php';
  }

When i log in as admin and open from the menu the worker site it shows me the engineer menu. I tried different ways of IF statements but no luck. Also if there is only one include header it works as it should but i want the admin user to have access to all sites and keep the header for the admin not to be changed when it goes to worker site with the engineer header. Thank you for your patience and fast response.

Thanks

Advertisement

Answer

Based on your question only 1 include should be needed and which one depends on the role set in the session variable named role. That said, this should work for you.

$role = ($_SESSION["role"] ?? '');
if ($role == 'engineer') {
    include 'engineerheader.php';
  } else if ($role == 'admin') {
    include 'adminheader.php';
 } else {
    include 'workerheader.php';
  }

You could also do it with a switch statement which would make it easier to modify if additional roles are needed or are taken away.

$role = ($_SESSION["role"] ?? '');
switch ($role) {
    case 'engineer':
        include 'engineerheader.php';
        break;
    case 'admin':
        include 'adminheader.php';
        break;
    default:
        include 'workerheader.php';
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement