- Home /
Getting info using WWW & PHP
Hello everyone, thanks for taking the time to look at my post.
I'm looking into doing a Register, Login, Purchases & Leaderboards system using Unity, C#, WWW, PHP & PDO for MySQL connections.
I currently have it setup, but am having issues just getting one piece of information from my WWW.
Here's my PHP Script:
// Get our DB settings, call databsae using $db
require 'dbsettings.php';
// Assign the information from the URL.
$user = $_POST['username'];
$pass = $_POST['password'];
// Check we've had the information we need.
if(isset($user) && isset($pass)) {
// Check username exists and password matches hash in database.
$checkuser = $db->query("SELECT * FROM Users WHERE Username = '".$user."'");
// Get results returned, we want none.
$row_count = $checkuser->rowCount();
// Did our query return no results?
if ($row_count == 0) {
// No results, User not found.
echo "2";
} else {
$user = $checkuser->fetch(PDO::FETCH_OBJ);
// Hash our users password, check it's the same.
if (crypt($pass, $user->Password) === $user->Password) {
// Our hash matched the database copy, all good.
echo "1";
} else {
// Hash doesn't match.
echo "3");
}
}
}
?>
This works perfectly, previously I was using GET and added a form so I could test it via a web browser. It's returning the correct information to Unity, but Unity isn't handling it correctly.
Here's my Unity script:
using UnityEngine;
using System.Collections;
public class LoginMenu : MonoBehaviour
{
string loginURL = "http://www.greenlightgames.co.uk/interceptis/login.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);
WWW download = new WWW (loginURL, form);
yield return download;
string newString = download.text.ToString ();
int result = int.Parse (newString.ToString ());
Debug.Log (download.text);
if (download.error != null) {
label = ("Connection error.");
} else {
if (download.text == "3") {
label = ("Password incorrect.");
}
if (download.text == "1") {
label = ("Logged In");
}
if (download.text == "2") {
label = ("Username not found.");
}
}
yield break;
}
}
When testing it in Unity, I'm receiving the correct information. Such as 2 user not found, 3 password incorrect and finally 1 username and passwords matched.
But my reader.text is returning a large ammount of spaces before reading the PHP's echo?
Here's a screenshot: http://puu.sh/cfRLD/b87e6e0117.png
I'm not sure why, but this is stopping my scripts from reading the returned data correctly. Could anyone help me fix this?
There is no HTML formatting in my PHP file.
Also, at a later point I want to store high scores and store purchases, can someone please help me RECEIVE more than one piece of information form the PHP?
For example.
PHP would return:
UserID - Score - Date/Time - Level:
3 - 3156 - 16:29 17/10/2014 - 4
I'd imagine that would come through one echo such as: 3, 3156, 16:29 17/10/2014, 4
How could I break this up and assign the values to custom variables?
Thank you for your help!
I've never integrated PHP with unity before, so not sure if this is even relevant. But as far as PHP is concerned, normally you dont echo results, you return them. And I'd also suggest trying to use ints ins$$anonymous$$d of strings.
// No results, User not found.
//echo "2";
return 2;
If I've misunderstood how your code works, and echoing is absolutely necessary, you should still try using ints ins$$anonymous$$d of strings, I think that would get rid of the unknown spaces. Extra good practice PHP tip: set up your statuses as constants at the top of the page.
const STATE_SUCCESS = 1;
const STATE_NO_RESULT = 2;
const STATE_BAD_HASH = 3;
//Some code
return STATE_NO_RESULT;
//$$anonymous$$ore code...
return STATE_SUCCESS;
//Even more code...
return STATE_BAD_HASH;
$$anonymous$$akes the code a lot more easier to read, removed the need for comments before the return/echo statements, and you no longer have to remember what number means what state after you defined them.
Answer by ExtremePowers · Oct 18, 2014 at 11:11 AM
You can use SimpleJSON, there is a plugin for unity with it (http://wiki.unity3d.com/index.php/SimpleJSON) So I make a array with all the data from the specific row in mysql, to get game score etc. Then I simply do
echo json_encode($jsonData);
at the end and voila its json, then you get the text in the game, then you use SimpleJSON to make it into an array.
Answer by Hartas · Oct 18, 2014 at 10:06 AM
Not completely sure but : retest with your GET method and check the html source and see if you find space. Check too the encoded (utf8 etc...). Check that dbsetting.php return nothing. Note the purpose but take care about security : see sql injection for more information. You can't use direct user input without testing/cleaning it. For request and return more informations see serialize or return a xml/json and parse it with unity.
I hope it will help ;)
Answer by KpjComp · Oct 18, 2014 at 11:02 AM
In your PHP script are you sure that your first line begins with ->
Your answer
Follow this Question
Related Questions
WWWForm: can't get it to work with Slack Webhooks 1 Answer
PHP POST through $_REQUEST with WEBBUILD 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
POST a form over HTTPS with unvalidated SSL Certificate 2 Answers
After updating to v2017.1, a www call returns "406 not acceptable" 2 Answers