PHP - If statement always evaluating as true?

radiocaf

So I can't get my head around this if statement.

What I am trying to do is get the person logged in from $_SESSION["user_name"] and if it matches one of two possible "admin" users, to give extra functionality. But it's always firing true.

For the example I'm going to have three users: Emma, John and Robert. Emma and John should be allowed the extra "admin functionality", but Robert is not allowed it. So...

My statement is:

if ($_SESSION["user_name"] === "emma" or "john"){
//give extra functionality as well as basic functionality
} else {
//give just basic functionality

But this is always firing true, so when I log in as my test account with the username Robert, he is also getting the extra functionality.

I've tried changing the quotation marks " of $_SESSION to apostrophes ', and also tried changing the operator from === to ==. I even tried = but found the hard way that this was setting my $_SESSION variable to "emma".

What am I doing wrong because I just can't seem to get my head around it?

If it's worth noting, this if statement is contained in a parent if statement that uses colons : and endif rather than brackets {}. The parent if statement is purely there to decide on what functionality to output based a column returned being empty or a user's name in there.

jpschroeder

You need to redeclare the full condition:

if ($_SESSION["user_name"] === "emma" or $_SESSION["user_name"] === "john"){
    //give extra functionality as well as basic functionality
} else {
   ...
}

Update With Explanation:

The pseudo code syntax for if statements is:

if ([some condition] [and/or] [another condition]) {
    // then do some stuff
}

each of those [condition] statements is evaluated as either "truthy" or "falsey". So your original question could be re-written something like this:

if ([$_SESSION["user_name"] is equal to "emma"] or ["john" is not false, 0, or null]) {
    // we will always get in here
}

Since "john" will always be "truthy" (it is not false, null, 0) it will always pass the condition.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is this Smarty if statement always evaluating true?

From Dev

if condition always evaluating true

From Dev

PHP if statement always TRUE when comparing strings

From Dev

PHP Why my 'if' statement always gives TRUE?

From Dev

If or statement in php not evaluating correctly

From Dev

If statement is always true

From Dev

If statement always returning true?

From Dev

PHP if statement evaluating to false, proceeds into executing true code. am i missing something?

From Dev

Why is this if statement always returning true

From Dev

'else if' statement always returns true

From Dev

If statement always true with enum in comparison

From Dev

Why is this if statement always returning true

From Dev

pdo statement always return true

From Dev

If statement always returning true in django

From Dev

If statement is always coming up true

From Dev

Evaluating requirements in python using string.isalpha, return always True

From Dev

Evaluating requirements in python using string.isalpha, return always True

From Dev

javascript/codecademy - if statement runs twice as false before evaluating true?

From Dev

Why is this IF statement evaluating to false when all conditions are true?

From Dev

Short hand if statement always returning true

From Dev

If-statement always returning true in jQuery

From Dev

Why is my JavaScript if statement always true?

From Dev

First if statement always true, but second one is not

From Dev

C++ GUI Condition for if statement always true

From Dev

Why is this JavaScript if statement always returning true?

From Dev

Simple if-else statement always returns true

From Dev

How to make if statement always returns true for Rspec?

From Dev

Greasemonkey: Why does this if statement always return true?

From Dev

If statement always True Jinja2 Template

Related Related

  1. 1

    Why is this Smarty if statement always evaluating true?

  2. 2

    if condition always evaluating true

  3. 3

    PHP if statement always TRUE when comparing strings

  4. 4

    PHP Why my 'if' statement always gives TRUE?

  5. 5

    If or statement in php not evaluating correctly

  6. 6

    If statement is always true

  7. 7

    If statement always returning true?

  8. 8

    PHP if statement evaluating to false, proceeds into executing true code. am i missing something?

  9. 9

    Why is this if statement always returning true

  10. 10

    'else if' statement always returns true

  11. 11

    If statement always true with enum in comparison

  12. 12

    Why is this if statement always returning true

  13. 13

    pdo statement always return true

  14. 14

    If statement always returning true in django

  15. 15

    If statement is always coming up true

  16. 16

    Evaluating requirements in python using string.isalpha, return always True

  17. 17

    Evaluating requirements in python using string.isalpha, return always True

  18. 18

    javascript/codecademy - if statement runs twice as false before evaluating true?

  19. 19

    Why is this IF statement evaluating to false when all conditions are true?

  20. 20

    Short hand if statement always returning true

  21. 21

    If-statement always returning true in jQuery

  22. 22

    Why is my JavaScript if statement always true?

  23. 23

    First if statement always true, but second one is not

  24. 24

    C++ GUI Condition for if statement always true

  25. 25

    Why is this JavaScript if statement always returning true?

  26. 26

    Simple if-else statement always returns true

  27. 27

    How to make if statement always returns true for Rspec?

  28. 28

    Greasemonkey: Why does this if statement always return true?

  29. 29

    If statement always True Jinja2 Template

HotTag

Archive