1

I am programming a login system now on Unity, but I have this weird bug. For some reason when I get the output of the php file, the if statement in the C# file can't see that 'loginreturn' (= AccountDoesntExist) and the string "AccountDoesntExist" are the same. I don't know why but maybe you smart people see the bug and can help me out.

C# Code:

IEnumerator TryLogin(string username, string password)
{
    WWWForm form = new WWWForm();

    form.AddField("username", username);
    form.AddField("password", password);

    WWW loginWWW = new WWW(LoginURL, form);
    yield return loginWWW;

    if (!string.IsNullOrEmpty(loginWWW.error))
    {
        Debug.LogError("Cannot connect to LOGIN servers! Error: " + loginWWW.error);
    }else
    {

        string loginreturn = loginWWW.text;
        Debug.Log(loginWWW.text);
        Debug.Log(loginreturn);

        if (loginreturn == "AccountDoesntExist")
            Debug.Log("WORKS!");

    }
}

PHP Code (which will always return "AccountDoesntExist" because of the way I log in):

<?php

$inputusername = $_REQUEST["username"];
$password = $_REQUEST["password"];

$servername = "localhost";
$username = "chatsystem_accs";
$password = "CENCORED";
$dbname = "chatsystem_accs";

//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

//Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//Connected successfully

$sql = "SELECT `username`, `password` FROM `accounts`";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($inputusername == $row["username"]) {
            if (password_verify($password, $row["password"])) {
                echo "Success";
            }else {
                echo "UsernameOrPasswordIncorrect";
            }
        }else {
            echo "AccountDoesntExist";
        }
    }
}else {
    echo "AccountDoesntExist";
}

//Close connection
$conn->close();

?>
Programmer
  • 121,791
  • 22
  • 236
  • 328
Ablos
  • 31
  • 1
  • 1
  • 12
  • Why does your `TryLogin` return IEnumerator? My guess is that the `yield return` in the middle of the method is having some unintended side effects. Seems like it should return `string` and omit the yield. – Cᴏʀʏ Oct 05 '16 at 17:33
  • I don't know, but I saw this in a tutorial (don't know which one anymore) – Ablos Oct 05 '16 at 17:34
  • @Cᴏʀʏ the yield return is needed by Unity to run the request on the background. Take it out and the whole application will freeze – Camilo Terevinto Oct 05 '16 at 17:36
  • 2
    Debug the code and check exactly what loginreturn contains. Probably not what you think – Camilo Terevinto Oct 05 '16 at 17:43
  • @CamiloTerevinto did that already, but it debugs exactly "AccountDoesntExist" – Ablos Oct 06 '16 at 15:05

1 Answers1

0

Very likely a UTF-8 problem. There is an extra data in the received bytes. Convert the received data to UTF-8 before comparing it.

Replace

string loginreturn = loginWWW.text;

with

string loginreturn  = System.Text.Encoding.UTF8.GetString(loginWWW.bytes, 3, loginWWW.bytes.Length - 3);

EDIT:

Did debugging like this:

Debug.Log("Received Length: "+ loginreturn.Length);
Debug.Log("Compare Length: " + "AccountDoesntExist".Length);

and the results were:

Received Length: 19

Compare Length: 18

This is wrong. There is an extra character somewhere. Debugged again with the function below then called it with displayAsChar(loginreturn);

void displayAsChar(string badValue)
{
    char[] values = badValue.ToCharArray();
    for (int i = 0; i < values.Length; i++)
    {
        string tempResult = "Value at index " + i + " is: " + values[i];
        Debug.Log(tempResult);
    }
}

It shows there is an empty character at the end of the character. I thought that was just " " but it wasn't.

I made another function to see what this empty character is:

void showStringAsHex(string badValue)
{
    foreach (char c in badValue)
        Debug.Log("To Unicode: " + ((int)c).ToString("X2"));
}

Bingo. That last character is 0A (Hex) which is also represented as \n. This is used as a line feed.

FIX:

Before doing the compare action, trim the string. This will remove any escape character and empty strings in the beginning and end of the character.

To trim the character, simply add the code below before comparing the string.

loginreturn = loginreturn.Trim();

Why post the debugging process?

The characters might be different for different servers. Posting this will help others troubleshoot and fix this problem for them-selves in the future.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I tried it but now the string I get out of the php script is: "ountDoesntExist". When I compare "ountDoesntExist" with "ountDoesntExist" it still doesn't reconize it. – Ablos Oct 06 '16 at 15:04
  • How about this? Create a temporary account I can use to test this hen give it to me here. I will be willing to test this on my side and let you know right away. You should also provide what Unity version you are running and what Os you are testing this on – Programmer Oct 06 '16 at 15:08
  • well I run Unity version 5.4.1 (newest version) and windows 10. I had this exact same system before (one Unity version earlier) and than it worked perfectly. It is a bit weird. – Ablos Oct 06 '16 at 15:26
  • But what do you mean with temporary account? For php? or for unity? because for unity I can just add you to my collaberation service so you can download it. – Ablos Oct 06 '16 at 15:27
  • The function is connecting to a server. Right? The `TryLogin` function uses a url, username and password to communicate with the php script? I need those three so that I can try this here and see what's the problem. This is how I was able to help another user [here](http://stackoverflow.com/a/39489237/3785314). Without those information I cannot replicate your problem.... If this is a local server, you use port forward to make it accessible out of your local network. That's fine if you don't want to do it. – Programmer Oct 06 '16 at 15:33
  • What is the url you use in the code? the username you use in the code and the password? For example, the username is.....the pass is....the url is...... – Programmer Oct 06 '16 at 15:43
  • sorry accedently pressed enter :D – Ablos Oct 06 '16 at 15:44
  • lol i was confused with that reply.Ok. will get to it in minutes – Programmer Oct 06 '16 at 15:45
  • Check the **Edited** Answer and the fix. You can now remove the temporary username and password from your server. Don't forget to accept answer if problem is solved. – Programmer Oct 06 '16 at 22:33
  • Thank you so much for you help! – Ablos Oct 07 '16 at 08:26