Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bryanlincoln · Jan 04, 2015 at 04:30 PM · javascriptloginphp

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!

Comment
Add comment · Show 15
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · Jan 04, 2015 at 04:33 PM 0
Share

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.

avatar image bryanlincoln · Jan 05, 2015 at 12:35 PM 0
Share

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.

avatar image UnityKen · Jan 05, 2015 at 01:03 PM 0
Share

I might be wrong, but shouldn't var nome, be name?

avatar image bryanlincoln · Jan 05, 2015 at 01:12 PM 0
Share

Yes, it was wrong, but it still not working :(

avatar image UnityKen · Jan 05, 2015 at 01:20 PM 0
Share
      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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Jan 05, 2015 at 02:23 PM

 if (hs_post.data.Equals("ok")) {

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bryanlincoln · Jan 05, 2015 at 03:06 PM 0
Share

I've tried that too, it didn't work too

avatar image Graham-Dunnett ♦♦ · Jan 05, 2015 at 03:10 PM 0
Share

So what does Debug.Log(hs_post.data) print? And Debug.Log(hs_post.data.Length)?

avatar image Graham-Dunnett ♦♦ · Jan 05, 2015 at 03:11 PM 0
Share

Oh, and doesn't the WWW class have a text member and not a data member?

avatar image ArkaneX · Jan 05, 2015 at 03:19 PM 0
Share

data works, and internally return text. It is obsolete though, at least in Unity 4 - maybe you removed it in 5...?

avatar image Graham-Dunnett ♦♦ · Jan 05, 2015 at 03:19 PM 0
Share

Hmm, data is just the old name for text so ignore me.

Show more comments
avatar image
0

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!!! :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges