1

好的,所以我有以下代码:

    if ($AccountData ['nAuthID'] == 1 && $data['MAINT'] == True) {
    die ('MM');
}

这在技术上应该可行,但由于某种原因它不可行。

基本上在我的文本文件中,我已经说过 maint 是错误的,但是当它到达这部分代码时,它仍然会死掉。

我认为这与 '$data['MAINT'] == True' 部分有关,但我不确定它有什么问题?

以下是所有代码:

$exec = mssql_query("SELECT nEMID, nAuthID, sUserPass FROM tAccounts where sUsername = '$user'");
$AccountData = mssql_fetch_assoc($exec);
$file = file('LauncherInfo.txt');

foreach ($file as $line) {
    if ($line) {
        $splitLine = explode(' = ',$line);
        $data[$splitLine[0]] = $splitLine[1];
    }
}

//echo $data['MAINT'];

if ($AccountData ['nAuthID'] == 2 && $data['MAINT'] == True) {
    //Continue.
} else if ($AccountData ['nAuthID'] == -2) {
    die ('INV');
} else if ($AccountData ['nAuthID'] == -1) {
    die ('BAN');
} else if ($AccountData ['nAuthID'] == 1 && $data['MAINT'] == True) {
    die ('MM');
} else if ($AccountData ['nAuthID'] == 0) {
    die ('EVR');
}

这是文本文件“LauncherInfo.txt”中的内容:

IP = 127.0.0.1
EXE = Client.exe
PORT = 8080
TITLE = Title
MAINT = False
Maintenance-Message = The server is currently in maintenance.
Ban-Message = You have been banned sucker!
Email Verification-Message = You need to active your email adddress.
Investigation-Message = Your account is undergoing investigation.

编辑:

我尝试使用以下代码:

if ($AccountData ['nAuthID'] == -2) {
    die ('INV');
} else if ($AccountData ['nAuthID'] == -1) {
    die ('BAN');
} else if ($AccountData ['nAuthID'] == 0) {
    die ('EVR');
} else if ($AccountData ['nAuthID'] == 1 && $data['MAINT'] == 'True') {
    die ('MM');
} else if ($AccountData ['nAuthID'] == 2 && $data['MAINT'] == 'True') {
    //Continue and allow user to log in.
}

我也试过这个:

   if ($AccountData ['nAuthID'] == -2) {
        die ('INV');
    } else if ($AccountData ['nAuthID'] == -1) {
        die ('BAN');
    } else if ($AccountData ['nAuthID'] == 0) {
        die ('EVR');
    } else if ($AccountData ['nAuthID'] == 1 && $data['MAINT'] == "True") {
        die ('MM');
    } else if ($AccountData ['nAuthID'] == 2 && $data['MAINT'] == "True") {
        //Continue and allow user to log in.
    }

但这些都不起作用?

EDIT2:我将代码更改为如下所示:

    if ($AccountData ['nAuthID'] == -2) {
    echo ('INV');
} else if ($AccountData ['nAuthID'] == -1) {
    echo ('BAN');
} else if ($AccountData ['nAuthID'] == 0) {
    echo ('EVR');
} else if ($AccountData ['nAuthID'] == 1 && $data['MAINT'] == "True") {
    echo ('MM');
} else if ($AccountData ['nAuthID'] == 2 && $data['MAINT'] == "True") {
    //Continue and allow user to log in.
}

我在浏览器中看到的是 EVR。似乎它甚至没有从那里继续?

谢谢。

4

2 回答 2

2

您正在检查MAINT文本文件中是否为 True。您实际上是在比较字符串。如果你不将它括在引号中,PHP 会假定它是一个布尔值(它不是)。因此,您需要将其用双引号括起来,如下所示:

if ($AccountData ['nAuthID'] == 2 && $data['MAINT'] == "True") {
} else if ($AccountData ['nAuthID'] == -2) {
    die ('INV');
} else if ($AccountData ['nAuthID'] == -1) {
    die ('BAN');
} else if ($AccountData ['nAuthID'] == 1 && $data['MAINT'] == "True") {
    die ('MM');
} else if ($AccountData ['nAuthID'] == 0) {
    die ('EVR');
}

希望这可以帮助。

于 2013-06-27T01:13:45.507 回答
1

file()将换行符留在字符串中,因此$data['Maint']contains "True\n",而不是"True". 您可以使用该FILE_IGNORE_NEW_LINES选项来防止这种情况:

$file = file('LauncherInfo.txt', FILE_IGNORE_NEW_LINES);

另一种选择是在处理之前修剪行 - 这也将删除尾随空格。

foreach ($file as $line) {
    $line = trim($line);
    if ($line) {
        $splitLine = explode(' = ',$line);
        $data[$splitLine[0]] = $splitLine[1];
    }
}

然后您还需要与"True", not进行比较True,如 Amal Murali 的回答:

if ($AccountData ['nAuthID'] == 2 && $data['MAINT'] == "True") {

您遇到的另一个问题是您从 SQL 查询中得到一个错误,而不是检查它。调用后mssql_query(),执行:

if (!$exec) {
    die('MSSQL error: ' . mssql_get_last_message());
}

这将显示来自 MSSQL 的错误消息。

于 2013-06-27T01:28:15.290 回答