-1

Currently trying to do something if user has x role he shouldn't be allowed on the page and it should exit().

I currently have tried this

if ($role != 'Host' OR 'Admin') {
exit('You do not have permission to access this page!');}

But it keeps exiting even when the current user has the $role = Host or Admin.

In my mind this all makes sense but the code just doesn't work.

LeonKong
  • 1
  • 2
  • 3
    `if ($role != 'Host' OR 'Admin') {` should be `if ($role !== 'Host' || $role !== 'Admin') {` – Guido Faecke Aug 08 '22 at 14:40
  • @GuidoFaecke why can't I just use the OR statement too check both at once rather then repeating it? – LeonKong Aug 08 '22 at 14:41
  • Because `OR` is another way to do `||` - you could have `if ($var1 == "hello" OR $var2 == "goodbye")`, it doesn't know which var is which, only 2 separate checks – Can O' Spam Aug 08 '22 at 14:41
  • Shorthand that is exactly the same length – mousetail Aug 08 '22 at 14:42
  • 2
    Each side of the OR statement must be a full statement. So `$role != 'Host' OR 'Admin'` can be read as `($role != 'Host') || 'Admin')`, so `Admin` will always be true. My favorite for checks like this is `if(!in_array($role, ['Host','Admin']))`. It's a little cleaner and easier to read. – aynber Aug 08 '22 at 14:43
  • It keeps giving me the exit function even when I have changed the code into @GuidoFaecke's answer. – LeonKong Aug 08 '22 at 14:43
  • In your version PHP check if `$role != 'Host'` is true OR `'Admin'` is `true`. Essentially these are 2 different comparisons. – Guido Faecke Aug 08 '22 at 14:44
  • 2
    Also, OR statements, only one has to be true, so even if $role is Admin, it's not Host, so it will hit the exit. you'd need AND, not OR – aynber Aug 08 '22 at 14:44
  • It's not uncommon for languages to require multiple tests for a single value rather than a single test for multiple values. It is less ambiguous and allows for some pretty complicated statements without the result getting interpreted in a way the user was not expecting. – arresteddevelopment Aug 08 '22 at 14:45
  • @aynber that fixed it however I find it very strange. I don't really get why it should be AND and not OR. – LeonKong Aug 08 '22 at 14:46
  • Because on an OR clause, only 1 needs to match. For AND, all need to match. A role cannot be both Host and Admin, so one side of the OR statement will always fail. If you were using `if($role == 'Admin' || $role == 'Host')`, that's fine because you're trying to prove a positive. When you're trying to prove a negative, then you need to prove all of the clauses, since $role will either be Host OR Admin or none of the above, but if role is Host, then it's not Admin, and vice versa – aynber Aug 08 '22 at 14:49
  • Another way to look at it. Say $role is Host. `$role != 'Host'` is false. Great. Now it has to check the other side of the OR, and `$role != 'Admin'` is true. `false || true` is true because only one side needs to match. `false && true` is false, because they both need to match. – aynber Aug 08 '22 at 14:51
  • @CanO'Spam You need to enclose each statement with parenthesis because of the `or` operator precedence. – Markus Zeller Aug 08 '22 at 14:51
  • That actually makes so much sense, thanks @aynber. – LeonKong Aug 08 '22 at 14:52

1 Answers1

0

What you want is this:

if ($role != 'Host' AND $role != 'Admin') {
    exit('blabla');
}

But, it is better to put the allowed roles not directly into the condition but into an array, for instance.

$allowed_roles = ['Host', 'Admin'];
if (! in_array($role, $allowed_roles) {
    exit('blabla');
}

The main advantage of this is that you can store the allowed roles somewhere else but in your logical code, e.g. in a database or in a config file. And you can easily change the list without touching your program's logic.

And, to explain why your code always keeps exiting: The right hand side part of your if ($role != 'Host' OR 'Admin') is always true. That is because a non-empty string like 'Admin' will evaluate to true. That's why the part on the right hand side of the OR is always true, and so the whole expression is always true (OR means that only one of multiple conditions has to be true).

Andreas
  • 76
  • 8
  • By the way: `OR` is not a variable, but a logical operator. – Andreas Aug 08 '22 at 22:59
  • Please help Stack Overflow to combat redundant questions/content by flagging to close duplicate questions. As a general rule, if you see a basic question asked after 2015, it has probably been asked and answers at least 5 times already. – mickmackusa Aug 08 '22 at 23:53
  • Ok, thank you for the hint. I'll do so in future. – Andreas Aug 09 '22 at 06:57