Jason
Jason

Reputation: 219

Or statement isn't functioning properly

I want to be redirected if I don't have an account1 or if I don't have an account2. The problem is, even if I have account1 it is redirecting me:

if(($_SESSION['account'] != "account1") || ($_SESSION['account'] != "account2")){
    header("location:/home");
    exit();
}

It works properly and doesn't redirect me when I have account1 when I don't include the OR:

if($_SESSION['account'] != "account1"){
    header("location:/home");
    exit();
}

but I also need it to not redirect if they, by chance, have account2.

What am I doing wrong?

Upvotes: 0

Views: 58

Answers (4)

Rob
Rob

Reputation: 1143

Your condition is wrong :) Assuming that you have an account2, the first comparison is true, and you get redirected. You need to combine the conditions with AND (&&).

Upvotes: 2

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You need

if(($_SESSION['account'] != "account1") && ($_SESSION['account'] != "account2")){
    header("location:/home");
    exit();
}

Upvotes: 2

Jon
Jon

Reputation: 437386

The logic is wrong; you probably meant AND instead of OR. $_SESSION['account'] can only have one value at any given time, so it's always going to be different than account1 OR different than account2.

So the code should read

if(($_SESSION['account'] != "account1") && ($_SESSION['account'] != "account2")){
    header("location:/home");
    exit();
}

Upvotes: 5

Simon Germain
Simon Germain

Reputation: 6844

I believe you want to check ($_SESSION['account'] != "account2"). You seem to have them reversed in the second condition.

Upvotes: -1

Related Questions