Pregunta

Hi I'm having a problem with this line of code.

if($_SESSION['permission'] !=  "superadmin" || "admin"){
    //header("location:logout.php");
    echo $_SESSION['permission'];
}

It is a quick line that would logout the user if the access level is not superadmin or admin but my if else is ignoring the "admin" after the || operator. Would appreciate any help on what could I be doing wrong. Thanks. Also tried && and it still doesn't work.

¿Fue útil?

Solución

just fix your if to the following :

if($_SESSION['permission'] !=  "superadmin" &&  $_SESSION['permission'] != "admin"){

in your code you have two conditions

$_SESSION['permission'] !=  "superadmin" || "admin"

and this is interpreted like :

$_SESSION['permission'] !=  "superadmin"
OR 
"admin"

and the condition

 "admin"

is equvilant to true because its none empty value so it will always pass the if statment

Otros consejos

The correct code :

if($_SESSION['permission'] !=  "superadmin" && 
$_SESSION['permission'] !=  "admin" 
){
             //header("location:logout.php");
             echo $_SESSION['permission'];
        }

try this

    if(($_SESSION['permission'] !=  'superadmin') && ($_SESSION['permission'] != 'admin'))   
     {
         //header("location:logout.php");
         echo $_SESSION['permission'];
    }

PHP use "short-circuit" for OR (even for AND, and other logical operator) statement.
That means - in case of OR operator - that if first condition is TRUE, second isn't evaluated.
Moreover, I'm not sure that your syntax is "good"; that's because (regardless short-circuit) first two strings will be compared (and the result will be "superadmin"; this is because two string are two true value, since they have a value and aren't null) and, then, this value will be compared with your session variable

Take a look on PHP manual

If you want to rewrite it correctly, you have to do something like this

if($_SESSION['permission'] != "superadmin" && $_SESSION['permission'] != "admin")
{
  //do something
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top