Reputation: 189
I have my $_SESSION['ugid']=1
.But why it still falls into the IF block?
Something wrong with my condition statement?
if(($_SESSION['ugid']!="1")||($_SESSION['ugid']!="2"))
{
$_SESSION['error']="access";
header("location:../error.php");
}
Upvotes: 1
Views: 108
Reputation: 2352
if you have an if statement like this:
if ( expr1 || expr2 ) {
// do something
}
And the first condition, expr1
is false, then PHP will evaluate the second condition. In this case, $_SESSION['ugid'] != "2" is true
, so it executes the code in the if block.
The answer depends on what you're trying to do. If 1
or 2
are the only valid values (i.e. anything other than those values is an error in your application), then you could replace your ||
with an &&
. I think it might read a bit nicer to do something like this:
$valid_guids = array(1, 2);
if ( !in_array($_SESSION['guid'], $valid_guids) ) {
// Error code
}
Upvotes: 0
Reputation: 55
Maybe having the value of your session as double qouted. Try removing it :)
Upvotes: 1
Reputation: 4470
please try this
if(($_SESSION['ugid']!=1)||($_SESSION['ugid']!=2))
{
$_SESSION['error']="access";
header("location:../error.php");
}
Upvotes: 1
Reputation: 1337
Because if it's 1 it's still different than 2 (because of the OR). I think you meant to put &&
.
Upvotes: 1
Reputation: 13672
Simply because you are using an OR condition.
if(($_SESSION['ugid']!="1")||($_SESSION['ugid']!="2"))
This reads as
if session variable 1 doesn't equal 1 **OR** session 2 variable doesn't equal 2
Clearly, session 2 variable is not equal to 2, therefore you are entering this code block.
Upvotes: 2
Reputation: 2802
Use && instead of ||
if(($_SESSION['ugid']!="1")&&($_SESSION['ugid']!="2"))
{
$_SESSION['error']="access";
header("location:../error.php");
}
Upvotes: 1