avgcoder
avgcoder

Reputation: 392

PHP and function

Okay so I have the following code:

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

That should technically work but for some reason it doesn't.

Basically in my text file I have said that maint is false but when it gets to this part of the code it still dies.

I think it's something to do with the '$data['MAINT'] == True' part but I am not sure what's wrong with it?

Here is all of the code:

$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');
}

This is what is inside the text file '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.

EDIT:

I tried using the following codes:

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.
}

I also tried this:

   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.
    }

But neither of those work?

EDIT2: I changed my code to look like this:

    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.
}

And what I see in the webbrowser is EVR. It seems like it doesn't even carry on from there?

Thanks.

Upvotes: 1

Views: 79

Answers (2)

Barmar
Barmar

Reputation: 781370

file() leaves the newlines in the strings, so $data['Maint'] contains "True\n", not "True". You can use the FILE_IGNORE_NEW_LINES option to prevent this:

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

Another option is to trim the line before processing it -- this will remove trailing spaces as well.

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

Then you also need to compare with "True", not True, as in Amal Murali's answer:

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

The other problem you're having is that you're getting an error from the SQL query, and not checking for it. After calling mssql_query(), do:

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

This will show the error message from MSSQL.

Upvotes: 1

Amal Murali
Amal Murali

Reputation: 76656

You're checking if MAINT is True in the text file. You're essentially doing a comparison of strings. If you don't enclose it in quotes, PHP assumes it's a boolean (it's not). So you'll need to enclose it in double quotes like so:

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');
}

Hope this helps.

Upvotes: 2

Related Questions