- Home /
Unityscript PHP Login problem
I have built a simple login system. I am using a php file to communicate with the database. The scenario I need is to do the login and if login is successful then load the mission. I am being able to login without a problem but for some reason unityscript is not evaluating/passing a simple condition. I have been trying to figure it out since two days. This is my snippet:
function login(w : WWW) {
yield w;
if (w.error == null) {
SendMessage("User", user);
if (w.text === "Logged in.") {
Debug.Log("Login Successful");
Application.LoadLevel("PrincipalScene");
} else {
message += w.text;
Debug.Log(w.text);
}
} else {
message += "Error: " + w.error + "\n";
}
}
The problem is in the condition if (w.text === "Logged in.")
. It is not passing for some reason. My php snippet is like this:
if ($count == 0) {
echo ("User does not exist. \n");
} else if (($count == 1) && ($pass == $row['password'])) {
echo("Logged in.");
} else {
echo("Password does not match. \n");
}
I would be grateful if anyone could help in this.
It prints "Logged in.". But I want to see "Login successful" and start of the scene but it does not enter that block.
Ah, don't use a triple equals, it should be a double == for comparison and single = for assignment. This isn't actually JavaScript unity is using.
I have tried that also but it didn't work. It is really strange this problem. Thank you.
Perhaps there are hidden characters, equals checks for explicit characters, try contains.
if (w.text.ToLower().Contains("logged in"))
Answer by Bunny83 · Oct 26, 2014 at 06:33 PM
w.text most likely contains some new line characters which you simply don't see. If you compare strings they have to match 100%.
Try this:
// UnityScript
// ...
var result = w.text.Trim(" "[0], "\n"[0], "\r"[0], "\t"[0]);
if (result == "Logged in.")
// ...