如何阻止两个回声出现(php)

时间:2012-08-10 17:21:09

标签: php

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 个答案:

答案 0 :(得分:2)

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

while($stmt->fetch()) {

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

一旦您知道自己获得了有效的结果,就可以检查该帐户是否需要激活。

在伪代码中:

if ($stmt->fetch()) {
    if ($row->$dbActive) {
        # valid login
    } else {
        # needs activation
    }
} else {
    # invalid login
}

答案 1 :(得分: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...";
}

答案 2 :(得分:0)

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

答案 3 :(得分: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";
}