2

So I'm trying to make a simple login system in Unity using C#, PHP and MySQL on localhost, but there is a problem. My code works like this:

In the php file, if the username and password matches the ones in the MySQL database, then echo 1. Else echo 0.

In the C# script file in Unity, the problem is in this part of the code:

if (www.error == null) 
    {
        if (www.data == "1") 
        {
            Debug.Log("Successful login/nLoading Scene...");
            StartCoroutine(GoToMenu());
        }
        else
        { 
            Debug.Log("Invalid account or password");
            print(www.url);
            print(www.data);
        }
    }

The problem is that my www.data is returning 1, but it's skipping the condition www.data == "1" and going to else, where it print it's URL (I checked it, it's correct and functional) and it's data (equal to 1).

2 Answers2

0

From your last comment it is obvious that your PHP script is puting special character into the responce string.

The best way would be to ensure that you use same encoding on server and client and not adding the special characters.

But the fastest way to do this is to remove all of them on client side with method like this: (source)

public static string RemoveSpecialCharacters(string str) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in str) {
      if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_') {
         sb.Append(c);
      }
   }
   return sb.ToString();
}

I am quessing that this special character is "\n"

Maybe this is not what you were looking for, but still it is some solution.

Community
  • 1
  • 1
Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36
  • I tried this but my www.text.Length still 2. But you're right about the encoding. I saved my php file as ASCI and UTF-8, and the output on Unity for www.text.Length was 2 and 3 respectively. But using the same format and encoding for the C# and PHP files doesn't output www.text.Length as 1. Oddly, if i use RemoveSpecialCharacters(www.data) instead of www.data or RemoveSpecialCharacters(www.text) the login will succeed even if the Length is > 1. So your answer save my day, thank you so much! – Ênio José Jan 22 '16 at 18:07
0

Try this

if (www.text == "1")

Or

if((int)www.text==1)
Ahmad
  • 5,551
  • 8
  • 41
  • 57
Eugene
  • 151
  • 8