Search Unity

C# Login Script doesn't work (Unity 2017.4). "UnityWebRequest"

Discussion in 'Scripting' started by Cellenseres, Jan 14, 2019.

  1. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Hey Guys,

    I'm trying to get a Login Script to work in Unity 2017.4. but it doesn't send the $_POST to my PHP-Script.
    The PHP-Script does work fine in Browser, but I can't get the $_POST send from Unity to PHP.

    This is the Script I use:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class LoginScript : MonoBehaviour
    6. {
    7.  
    8.     string loginURL = "http://cellenseres.de/includes/gamelogin.inc.php";
    9.     string username = "";
    10.     string password = "";
    11.     string label = "";
    12.  
    13.     void OnGUI()
    14.     {
    15.         GUI.Window(0, new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2 - 70), LoginWindow, "Login");
    16.     }
    17.  
    18.     void LoginWindow(int windowID)
    19.     {
    20.         GUI.Label(new Rect(140, 40, 130, 100), "Username");
    21.         username = GUI.TextField(new Rect(25, 60, 375, 30), username);
    22.         GUI.Label(new Rect(140, 92, 130, 100), "Password");
    23.         password = GUI.PasswordField(new Rect(25, 115, 375, 30), password, '*');
    24.  
    25.         if (GUI.Button(new Rect(25, 160, 375, 50), "Login"))
    26.         {
    27.             if (username != "" || password != "")
    28.             {
    29.                 StartCoroutine(HandleLogin(username, password));
    30.             }
    31.             else
    32.             {
    33.                 label = "Please fill in both fields.";
    34.             }
    35.         }
    36.  
    37.         GUI.Label(new Rect(55, 222, 250, 100), label);
    38.     }
    39.  
    40.     IEnumerator HandleLogin(string username, string password)
    41.     {
    42.         label = "Checking username and password.";
    43.         StartCoroutine(Login(username, password));
    44.         yield break;
    45.     }
    46.  
    47.     IEnumerator Login(string username, string password)
    48.     {
    49.         Debug.Log("Function Ran..");
    50.  
    51.  
    52.         WWWForm form = new WWWForm();
    53.         form.AddField("username", username);
    54.         form.AddField("password", password);
    55.         UnityWebRequest www = UnityWebRequest.Post(loginURL, form);
    56.         www.chunkedTransfer = false;
    57.         yield return www.SendWebRequest();
    58.  
    59.  
    60.         string newString = www.downloadHandler.text.ToString();
    61.         Debug.Log("New String:" + newString);
    62.         int result = int.Parse(newString); //THIS GIVES ERROR, BITCH
    63.  
    64.         Debug.Log(www.downloadHandler.text);
    65.  
    66.         if (www.error != null)
    67.         {
    68.             label = ("Connection error.");
    69.         }
    70.         else
    71.         {
    72.  
    73.             if (www.downloadHandler.text == "3")
    74.             {
    75.                 label = ("Password incorrect.");
    76.             }
    77.  
    78.             if (www.downloadHandler.text == "1")
    79.             {
    80.                 label = ("Logged In");
    81.             }
    82.  
    83.             if (www.downloadHandler.text == "2")
    84.             {
    85.                 label = ("Username not found.");
    86.             }
    87.         }
    88.  
    89.         yield break;
    90.     }
    91.  
    92. }
    93.  
    it ALWAYS sends a "0" back to Unity, which means that either $username or $password was not sent to the PHP via $_POST.
     
    Last edited: Jan 14, 2019
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Try setting useHttpContinue property to false.
     
  3. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class LoginScript : MonoBehaviour
    6. {
    7.  
    8.     string loginURL = "http://cellenseres.de/includes/gamelogin.inc.php";
    9.     string username = "";
    10.     string password = "";
    11.     string label = "";
    12.  
    13.     void OnGUI()
    14.     {
    15.         GUI.Window(0, new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2 - 70), LoginWindow, "Login");
    16.     }
    17.  
    18.     void LoginWindow(int windowID)
    19.     {
    20.         GUI.Label(new Rect(140, 40, 130, 100), "Username");
    21.         username = GUI.TextField(new Rect(25, 60, 375, 30), username);
    22.         GUI.Label(new Rect(140, 92, 130, 100), "Password");
    23.         password = GUI.PasswordField(new Rect(25, 115, 375, 30), password, '*');
    24.  
    25.         if (GUI.Button(new Rect(25, 160, 375, 50), "Login"))
    26.         {
    27.             if (username != "" || password != "")
    28.             {
    29.                 StartCoroutine(HandleLogin(username, password));
    30.             }
    31.             else
    32.             {
    33.                 label = "Please fill in both fields.";
    34.             }
    35.         }
    36.  
    37.         GUI.Label(new Rect(55, 222, 250, 100), label);
    38.     }
    39.  
    40.     IEnumerator HandleLogin(string username, string password)
    41.     {
    42.         label = "Checking username and password.";
    43.         StartCoroutine(Login(username, password));
    44.         yield break;
    45.     }
    46.  
    47.     IEnumerator Login(string username, string password)
    48.     {
    49.         Debug.Log("Function Ran..");
    50.  
    51.  
    52.         WWWForm form = new WWWForm();
    53.         form.AddField("username", username);
    54.         form.AddField("password", password);
    55.         UnityWebRequest www = UnityWebRequest.Post(loginURL, form);
    56.         www.chunkedTransfer = false;
    57.         www.useHttpContinue = false;
    58.         yield return www.SendWebRequest();
    59.  
    60.  
    61.         string newString = www.downloadHandler.text.ToString();
    62.         Debug.Log("New String:" + newString);
    63.         int result = int.Parse(newString); //THIS GIVES ERROR, BITCH
    64.  
    65.         Debug.Log(www.downloadHandler.text);
    66.  
    67.         if (www.error != null)
    68.         {
    69.             label = ("Connection error.");
    70.         }
    71.         else
    72.         {
    73.  
    74.             if (www.downloadHandler.text == "3")
    75.             {
    76.                 label = ("Password incorrect.");
    77.             }
    78.  
    79.             if (www.downloadHandler.text == "1")
    80.             {
    81.                 label = ("Logged In");
    82.             }
    83.  
    84.             if (www.downloadHandler.text == "2")
    85.             {
    86.                 label = ("Username not found.");
    87.             }
    88.         }
    89.  
    90.         yield break;
    91.     }
    92.  
    93. }
    94.  
    have added "www.useHttpContinue = false;" before "yield return www.SendWebRequest();"
    Still only getting a "0" back from the Server, which means that "$username = $_POST['username'];" is still empty.
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    What is returned when you var_dump the $_POST variables?
     
  5. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    upload_2019-1-14_14-1-44.png

    Here are some DebugInfos.
    TestUser and Testpassword are the inputs of the variables "username" and "password" which should be sent to my server via $_POST

    uploadedBytes : 0. This means, that nothing were uploaded ($_POST weren't sent to the server).
    www.domainHandler.text outputs "0". The echo I've setup for when no username/password were sent to the server ($username, $password are empty in my PHP script).

    This is the php script I'm using (far from finished):

    Code (CSharp):
    1. <?php
    2.  
    3.     require 'dbh.inc.php';
    4.  
    5.     $mailuid = $_POST['username'];
    6.     $password = $_POST['password'];
    7.  
    8.     if(empty($mailuid) || empty($password)){
    9.         echo '0'; //FIELD_EMPTY
    10.     }
    11.     else {
    12.         $sql = "SELECT * FROM users WHERE username=? OR email=?;";
    13.         $stmt = mysqli_stmt_init($conn);
    14.         if(!mysqli_stmt_prepare($stmt, $sql)){
    15.             echo '7'; //SQL_ERROR
    16.         }
    17.         else {
    18.             mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
    19.             mysqli_stmt_execute($stmt);
    20.             $result = mysqli_stmt_get_result($stmt);
    21.             if($row = mysqli_fetch_assoc($result)){
    22.                 $passCheck = password_verify($password, $row['password']);
    23.                 if($passCheck == false){
    24.                     echo '2'; //PW Wrong
    25.                 }
    26.                 else if($passCheck == true){
    27.                     echo '3'; //PW CORRECT
    28.                 }
    29.                 else {
    30.                     echo '5'; //UNKNOWN_ERROR
    31.                 }
    32.             }
    33.             else {
    34.                 echo '1'; //User Not found  
    35.             }  
    36.         }
    37.     }
    38. ?>
    39.  
     
  6. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    What if you immediately dump the post data on the server, and check that on your client. That way you can see exactly what was received.
     
  7. Cellenseres

    Cellenseres

    Joined:
    Mar 30, 2015
    Posts:
    68
    Mind telling me how to do that? Haven't done this before ^^'

    EDIT: Oh, yea var_dump in PHP.
    using: var_dump($_POST); gives me:

    upload_2019-1-14_15-18-0.png
     
    Last edited: Jan 14, 2019
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735