- Home /
Post login script works but Unity doesn't knows it
I have a login script that sends the information to the server and it answers if the credentials are right using a string. The script check the server's answer downloading the server's page content with .data and check what will it do.
The Unity's JS script:
private var secretKey="test";
var loginUrl="http://forum...";
var nome : String;
var password : String;
var senhaT : GameObject;
var Menu : GameObject;
var LoginP : GameObject;
var Login : boolean = false;
function login(name, password) {
var hash=md5.Md5Sum(name + password + secretKey);
var login_url = loginUrl + "name=" + WWW.EscapeURL(name) + "&password=" + password + "&hash=" + hash;
var hs_post = WWW(login_url);
yield hs_post;
if(hs_post.error) {
print("Error: " + hs_post.error);
senhaT.GetComponent("TextMesh").text = "Ocorreu um erro.";
yield WaitForSeconds(3);
senhaT.GetComponent("TextMesh").text = "Senha";
}
else
{
if(hs_post.data == "ok")
{
Menu.active = true;
LoginP.active = false;
Login = false;
PlayerPrefs.SetString("nome", name);
}
if(hs_post.data == "versao")
{
senhaT.GetComponent("TextMesh").text = "Download the latest version";
print(hs_post.data);
}
else
{
senhaT.GetComponent("TextMesh").text = "Wrong password";
print(hs_post.data);
}
}
}
The PHP server script:
$db = mysql_connect('myhost', 'myuser', 'mypass') or die('Cannot connect ' . mysql_error());
mysql_select_db('mydatabase') or die('Could not select database');
// Strings must be escaped to prevent SQL injection attack.
$name = mysql_real_escape_string($_GET['name'], $db);
$password = mysql_real_escape_string($_GET['password'], $db);
//$version = mysql_real_escape_string($_GET['version'], $db);
$hash = $_GET['hash'];
$secretKey="test";
$real_hash = md5($name . $password . $secretKey);
if($real_hash == $hash) {
// Send variables for the MySQL database class.
$qry="SELECT * FROM users WHERE User='".mysql_real_escape_string($name)."'AND Password='".$password."'";
$result=mysql_query($qry);
if (mysql_num_rows($result)>0) {
echo "ok";
}
else {
echo "wrong";
}
}
All the links are correct but I cannot find what's wrong. Everything works ok, but the JS script do not recognize the "ok" string and jumps to the else{} section.
Thanks!
Use Debug.Log to print the result of the WWW request. $$anonymous$$aybe the response has a carriage return or something like that on it.
Unfortunately, it hasn't. The response is clean but the JS script doesn't detect it, it jumps the if(data=ok) to the else {} part.
if(hs_post.data == "ok")
{
$$anonymous$$enu.active = true;
LoginP.active = false;
Login = false;
PlayerPrefs.SetString("nome", name);
It needs to be changed here also.
Answer by Graham-Dunnett · Jan 05, 2015 at 02:23 PM
if (hs_post.data.Equals("ok")) {
So what does Debug.Log(hs_post.data)
print? And Debug.Log(hs_post.data.Length)
?
Oh, and doesn't the WWW
class have a text
member and not a data
member?
data works, and internally return text. It is obsolete though, at least in Unity 4 - maybe you removed it in 5...?
Hmm, data
is just the old name for text
so ignore me.
Answer by bryanlincoln · Jan 05, 2015 at 11:29 PM
Ok, I've found a way to get it working. Since the data returned had another ghost character I changed this line:
if(hs_post.data=="ok") {
to:
if(hs_post.text.Contains("ok")) {
and it worked ok. Thanks guys, your help was essential to figure it out!!! :)
Your answer
Follow this Question
Related Questions
why wont my javascript work / wont open ? 0 Answers
Stopping a function when another function running. 2 Answers
A node in a childnode? 1 Answer
Program Crashes Unity 1 Answer
changing a coroutine's argument at the coroutine's runtime? 1 Answer