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

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:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class LoginScript : MonoBehaviour
{

    string loginURL = "http://cellenseres.de/includes/gamelogin.inc.php";
    string username = "";
    string password = "";
    string label = "";

    void OnGUI()
    {
        GUI.Window(0, new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2 - 70), LoginWindow, "Login");
    }

    void LoginWindow(int windowID)
    {
        GUI.Label(new Rect(140, 40, 130, 100), "Username");
        username = GUI.TextField(new Rect(25, 60, 375, 30), username);
        GUI.Label(new Rect(140, 92, 130, 100), "Password");
        password = GUI.PasswordField(new Rect(25, 115, 375, 30), password, '*');

        if (GUI.Button(new Rect(25, 160, 375, 50), "Login"))
        {
            if (username != "" || password != "")
            {
                StartCoroutine(HandleLogin(username, password));
            }
            else
            {
                label = "Please fill in both fields.";
            }
        }

        GUI.Label(new Rect(55, 222, 250, 100), label);
    }

    IEnumerator HandleLogin(string username, string password)
    {
        label = "Checking username and password.";
        StartCoroutine(Login(username, password));
        yield break;
    }

    IEnumerator Login(string username, string password)
    {
        Debug.Log("Function Ran..");


        WWWForm form = new WWWForm();
        form.AddField("username", username);
        form.AddField("password", password);
        UnityWebRequest www = UnityWebRequest.Post(loginURL, form);
        www.chunkedTransfer = false;
        yield return www.SendWebRequest();


        string newString = www.downloadHandler.text.ToString();
        Debug.Log("New String:" + newString);
        int result = int.Parse(newString); //THIS GIVES ERROR, BITCH

        Debug.Log(www.downloadHandler.text);

        if (www.error != null)
        {
            label = ("Connection error.");
        }
        else
        {

            if (www.downloadHandler.text == "3")
            {
                label = ("Password incorrect.");
            }

            if (www.downloadHandler.text == "1")
            {
                label = ("Logged In");
            }

            if (www.downloadHandler.text == "2")
            {
                label = ("Username not found.");
            }
        }

        yield break;
    }

}

it ALWAYS sends a “0” back to Unity, which means that either $username or password was not sent to the PHP via _POST.

Try setting useHttpContinue property to false.

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class LoginScript : MonoBehaviour
{

    string loginURL = "http://cellenseres.de/includes/gamelogin.inc.php";
    string username = "";
    string password = "";
    string label = "";

    void OnGUI()
    {
        GUI.Window(0, new Rect(Screen.width / 4, Screen.height / 4, Screen.width / 2, Screen.height / 2 - 70), LoginWindow, "Login");
    }

    void LoginWindow(int windowID)
    {
        GUI.Label(new Rect(140, 40, 130, 100), "Username");
        username = GUI.TextField(new Rect(25, 60, 375, 30), username);
        GUI.Label(new Rect(140, 92, 130, 100), "Password");
        password = GUI.PasswordField(new Rect(25, 115, 375, 30), password, '*');

        if (GUI.Button(new Rect(25, 160, 375, 50), "Login"))
        {
            if (username != "" || password != "")
            {
                StartCoroutine(HandleLogin(username, password));
            }
            else
            {
                label = "Please fill in both fields.";
            }
        }

        GUI.Label(new Rect(55, 222, 250, 100), label);
    }

    IEnumerator HandleLogin(string username, string password)
    {
        label = "Checking username and password.";
        StartCoroutine(Login(username, password));
        yield break;
    }

    IEnumerator Login(string username, string password)
    {
        Debug.Log("Function Ran..");


        WWWForm form = new WWWForm();
        form.AddField("username", username);
        form.AddField("password", password);
        UnityWebRequest www = UnityWebRequest.Post(loginURL, form);
        www.chunkedTransfer = false;
        www.useHttpContinue = false;
        yield return www.SendWebRequest();


        string newString = www.downloadHandler.text.ToString();
        Debug.Log("New String:" + newString);
        int result = int.Parse(newString); //THIS GIVES ERROR, BITCH

        Debug.Log(www.downloadHandler.text);

        if (www.error != null)
        {
            label = ("Connection error.");
        }
        else
        {

            if (www.downloadHandler.text == "3")
            {
                label = ("Password incorrect.");
            }

            if (www.downloadHandler.text == "1")
            {
                label = ("Logged In");
            }

            if (www.downloadHandler.text == "2")
            {
                label = ("Username not found.");
            }
        }

        yield break;
    }

}

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.

What is returned when you var_dump the $_POST variables?

4100299--358861--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):

<?php
  
    require 'dbh.inc.php';
  
    $mailuid = $_POST['username'];
    $password = $_POST['password'];
  
    if(empty($mailuid) || empty($password)){
        echo '0'; //FIELD_EMPTY
    }
    else {
        $sql = "SELECT * FROM users WHERE username=? OR email=?;";
        $stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)){
            echo '7'; //SQL_ERROR
        }
        else {
            mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
            mysqli_stmt_execute($stmt);
            $result = mysqli_stmt_get_result($stmt);
            if($row = mysqli_fetch_assoc($result)){
                $passCheck = password_verify($password, $row['password']);
                if($passCheck == false){
                    echo '2'; //PW Wrong
                }
                else if($passCheck == true){
                    echo '3'; //PW CORRECT
                }
                else {
                    echo '5'; //UNKNOWN_ERROR
                }
            }
            else {
                echo '1'; //User Not found  
            }  
        }
    }
?>

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.

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:

4100533--358894--upload_2019-1-14_15-18-0.png

Repeating my suggestion: Unity - Scripting API: Networking.UnityWebRequest.useHttpContinue

If setting that property to false doesn’t help, try using some debugging proxy like Fiddler to inspect the network traffic.