或声明无法正常运作

Jas*_*son 0 php

如果我没有帐户1或者我没有帐户2,我想被重定向.问题是,即使我有account1它重定向我:

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

它工作正常,当我没有包含OR时,当我有account1时它不会重定向我:

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

但如果他们偶然拥有account2,我也需要它不重定向.

我究竟做错了什么?

Jon*_*Jon 5

逻辑错了; 你可能意味着AND而不是OR.$_SESSION['account']在任何给定的时间只能有一个值,因此它总是与account1OR不同于account2.

所以代码应该读

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