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.