Skip to content
Advertisement

IF statement won’t take more than 2 conditions

I’m setting up a system where if a users group number is present the user will have access to the page. For example I want people in group 7,8, and 14 to have access to the page. This works fine for the users in group 7 and 8 but if the user is group 14 access appears to be denied. Here’s what I tried so far I tried to go this two ways but to no avail. Any help would be appreciated.

An if statment with more than two conditions.

if ($_SESSION['userrole'] == 8 || $_SESSION['userrole']== 7 || $_SESSION['userrole']== 14) {
//text 
}else{
echo "access denied";

An array that have values to check against

$acc = array("8","7","14");
    if (in_array($_SESSION['userrole'],$acc)){
//text 
}else{
echo "access denied";

Advertisement

Answer

as Syscall mentioned in his comment you are trying to look for the value “2,14” in array contain values like this array(“8″,”7″,”14”);
this is want work

what you need to do in this case is use explode()

in your scenario, it will look like this

$acc = array("8", "7", "14");
$user_group = explode(',',$_SESSION['userrole']);
foreach ($user_group as $group) {
    if (in_array($group, $acc)) {
       //text
    } else {
       //text
    }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement