1
while ($stmt->fetch()) {
    if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
        if ($dbActive == 1) {
            $loggedIn = true;
        } else if ($dbActive == 0) {
            $loggedIn = false;
            echo "You Must Activate Your Account from Email to Login";
        }
    }
}

if ($loggedIn == false && $_POST) {
    echo "The Username or Password that you Entered is not Valid. Try Entering it Again";
}

在上面的代码中,我在显示一条消息时遇到了问题,我似乎无法弄清楚我做错了什么。如果用户输入错误的密码或用户名,则会显示消息

"The Username or Password that you Entered is not Valid. Try Entering it Again".

这很好,但如果$dbActive= 0,它应该只显示消息

You Must Activate Your Account from Email to Login.

但相反,当发生这种情况时它会显示两条消息,因此它会显示

You Must Activate Your Account from Email to LoginThe Username or Password that you Entered is not Valid. Try Entering it Again.

我知道为什么会这样,但我似乎无法弄清楚。$dbActive如果= 0 ,如何阻止它显示两条消息?

4

4 回答 4

2

你似乎以一种非常奇怪的方式这样做。

while($stmt->fetch()) {

因此,您正在遍历数据库中的整个结果列表,检查每个结果是否与用户的详细信息匹配。为什么不在查询本身中包含详细信息?这样,您只需要检查数据库的返回 - 如果您得到返回的结果,您就知道这是一个有效的登录;如果什么都没有,您可以显示“无效登录”消息。

一旦你知道你得到了一个有效的结果,你就可以检查帐户是否需要激活。

在伪代码中:

if ($stmt->fetch()) {
    if ($row->$dbActive) {
        # valid login
    } else {
        # needs activation
    }
} else {
    # invalid login
}
于 2012-08-10T17:28:40.753 回答
0

这个逻辑可以更简单

从:

if ($dbActive == 1){
    $loggedIn = true;
} else if ($dbActive == 0) {
    $loggedIn = false;
    echo "You Must Activate Your Account from Email to Login";
}

到:

if ($dbActive == 1){
    $loggedIn = true;
} else{
    $loggedIn = false;
    echo "You Must Activate Your Account from Email to Login";
}

和你的最后一个条件

if ($loggedIn == false && isset($_POST['somefield']) {
    echo "The Username or Passwor...";
}
于 2012-08-10T17:26:06.390 回答
0
if ($loggedIn == false && !empty($_POST) && $dbActive != 0) {
    echo "The Username or Password that you Entered is not Valid. Try Entering it Again";
}
于 2012-08-10T17:27:10.610 回答
0

您的代码非常清楚地解释了一切:

while($stmt->fetch()) {
    if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
        if ($dbActive == 1){
            $loggedIn = true;
        } else if ($dbActive == 0) {


            $loggedIn = false;  

/****************************
 *
 * at this point, $loggedIn == false.
 *
 ****************************/


            echo "You Must Activate Your Account from Email to Login";
        }
    }
}

/****************************
 *
 * at this point, $loggedIn == false.
 *
 * since $loggedIn == false and $_POST is truthy, your if statement will get entered.
 *
 ****************************/

if ($loggedIn == false && $_POST) {
    echo "The Username or Password that you Entered is not Valid. Try Entering it Again";
}

那么我们如何解决这个问题呢?简单的!我们添加另一个变量来声明用户是否处于活动状态。

$active = true;

.
.
.

if ($dbActive == 0) {
    $loggedIn = false;
    $active = false;
    echo "You Must Activate Your Account from Email to Login";
}

.
.
.

if (!$loggedIn && $active && isset($_POST['formInputName']) {
    echo "The Username or Password that you Entered is not Valid. Try Entering it Again";
}
于 2012-08-10T17:28:16.853 回答