Something wrong with this IF statement

Hello all,

I’m braking my head for allmost two weeks with this, so I must be missing something very fundamental here.
The function below should check a login procedure(its only a part of the whole script), every thing seems to work fine.
Except that were the php script returns “wrong” or “right”; it does not get recognized by the If statement.
Any thoughts?

Thanks in advance, Robert

unction checkUser() {

    console = "Controle van inlog gegevens.";
    var getuser_url = getuserUrl + "?username=" + usernameText + "&password=" + md5functions.Md5Sum(passwordText);
    var cu_get = WWW(getuser_url);
    yield cu_get;
   
   if(cu_get.error) {
        console = "There was an error checking your username and password: " + cu_get.error;
    }
    else
    {print (cu_get.data);
		//although the PHP echo's out "right" this does not get evaluated
		//SO THERE IS SOMETHING WRONG WITH THIS IF STATEMENT???
       if(cu_get.data=="right")
	  
        {
           console = "Welcome back " ;
           yield WaitForSeconds(3.0);
           //userOK=true;
        }
       else
	   if (cu_get.data=="wrong")
	   {
	   print (cu_get.data);
	   console="Wrong Inlog";
	   }
	   else
	  
       {
          //only this one gets evaluated
          print (cu_get.data);
		  console = cu_get.data+"Something Fishy going on here";   
		  
       }
    }
   
    //CheckVersion();
}

Is it actually printing “right” exactly? Maybe it’s “right\n”? How about trying StartsWith(“right”) instead of == “right”?

–Eric

Thanks a lot Eric, this helps greatly.
The “StartsWith” does indeed do the trick.
Funny thing is that exactly “right” gets printed, without semicolons of course.
So I still would like to know how to solve this the right (with “==”) way.

Thanks for helping, Robert

Try this:

print (cu_get.data + "!");

If it prints “right !”, it’s putting in an extra space.
If it prints “!”, it’s putting in an extra carriage return.

In either case, you can try to strip out the offending character.

Strange; using that code, still gives back the same “right”

Thanks, Robert

Try this:

for (chr in cu_get.data) {
	Debug.Log (System.Convert.ToInt32(chr));
}

–Eric

Great Eric,

Using your code returned me “114,105,103,104,116,32,10”, so a space and line break are added.
I will try to strip that of in my php script.
B.t.w. in noticed that I have difficulty typing quotation marks in Monodevelop, is that typical behavior?
Right now I need to paste them in, I was looking at the settings to see if I could find something there, but I couldn’t.

Thanks, Robert

So I was too optimistic…
It seems to me that the extra characters aren’t from the php file, but are added somehow along the way, maybe even in unity, does that make sense?
What would be the way to strip them in Unity, with RegEx?

Thanks, Robert

If you know the length of the string you are looking for, you can use the .NET Substring function to extract the relevant part of it:-

var start = str.Substring(0, 5);  // Get first 5 characters.

If you are looking for a particular character in the string (such as the first space), you can use IndexOf to find its position and then use Substring to chop off the unwanted bit:-

var pos = str.IndexOf(" ");
var chopped = str.Substring(0, pos - 1);

Regexes are more powerful but usually overkill for things like this.

I noticed this a while ago, if you do a Debug.Log(“reply=”+reply+“…”); you’ll see that the reply comes with a space at the end so checking for “right” won’t work but "right " will :?