-2

I am trying to show a different navigation bar depending on a users authority. Only problem is that when i log on to the system it shows the first else if, regardless of the authority of the user. To ensure that the problem is in the loop i have tried switching the else ifs and the same happened. the code is in an external php file and i call the function in the top of each page. any suggestions ?

function checkAuth() {
  session_start();

  if(!isset($_SESSION['role'])) {
    require_once('menu.php');
  } else if ($_SESSION['role'] = "registered") {
    require_once('regnav.php');
  } else if ($_SESSION['role'] = "admin") {
    echo "FDGFGFD";
    require_once('adminnav.php');
  }

}
Albzi
  • 15,431
  • 6
  • 46
  • 63
Chanter
  • 99
  • 1
  • 12

2 Answers2

3

Your issue is with this part: $_SESSION['role'] = "registered". The single = means you are assigning the value "registered" to variable $_SESSION['role'].

If you are evaluating to check something, you need to use == i.e. $_SESSION['role'] == "registered"

You'll have the same issue with the second elseif

gabe3886
  • 4,235
  • 3
  • 27
  • 31
3

You need to use a double = sign for any condition check. For any condition check in if or else if, you have to use == in the middle of the variables.

If you use only = that means it assigning the value in the $_SESSION['role']. Also you can use === for checking the value as well as the type of the variable.

Valid function is:

function checkAuth()
{
session_start();


if(!isset($_SESSION['role']))
{
    require_once('menu.php');
}

else if ($_SESSION['role'] == "registered"){
    require_once('regnav.php');
}

else if ($_SESSION['role'] == "admin"){
    echo "FDGFGFD";
    require_once('adminnav.php');
}

}
?>
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42