Reputation: 23
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.
Upvotes: 0
Views: 71
Reputation: 29922
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
}
Upvotes: 0
Reputation: 1617
try this
if(($_SESSION['permission'] != 'superadmin') && ($_SESSION['permission'] != 'admin'))
{
//header("location:logout.php");
echo $_SESSION['permission'];
}
Upvotes: 0
Reputation: 3441
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
Upvotes: 2
Reputation: 3962
The correct code :
if($_SESSION['permission'] != "superadmin" &&
$_SESSION['permission'] != "admin"
){
//header("location:logout.php");
echo $_SESSION['permission'];
}
Upvotes: 0