Jump to content

What is Causing my Code to Not Return Any Data When Trying to Send a Login Request to a Local Server?


samsanders999

Recommended Posts

Hi,

For about a month, I have been trying to figure out why my code will not return anything after posting a wwwForm (I have also tried the newer equivalent of this function but I had no luck with that either.) The nameField and passwordField are taken from text boxes within the game and the code used in my login script is copied and pasted from a Register script but I have changed the file location to the login.php file. The register script works fine and I can add new users to my database but the login script only outputs "Form Sent." and not the "present" that should return when the form is returned and it never gets any further than that point meaning that it lets the user through with no consequence if they use an invalid name because the script never returns an answer. What should I do to fix this?

Thanks,

Unity Code:

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

public class Login : MonoBehaviour
{
    public InputField nameField;
    public InputField passwordField;

    public Button acceptSubmissionButton;

    public void CallLogInCoroutine()
    {
        StartCoroutine(LogIn());
    }

    IEnumerator LogIn()
    {
        WWWForm form = new WWWForm();
        form.AddField("username", nameField.text);
        form.AddField("password", passwordField.text);
        WWW www = new WWW("http://localhost/sqlconnect/login.php", form);
        Debug.Log("Form Sent.");
        yield return www;
        Debug.Log("Present");        if (www.text[0] == '0')
        {
            Debug.Log("Present2");
            DatabaseManager.username = nameField.text;
            DatabaseManager.score = int.Parse(www.text.Split('\t')[1]);
            Debug.Log("Log In Success.");
        }
        else
        {
            Debug.Log("User Login Failed. Error #" + www.text);
        }
        
    }

    public void Validation()
    {
        acceptSubmissionButton.interactable = nameField.text.Length >= 7 && passwordField.text.Length >= 8;
    }
}

login.php:

<?php
    echo "Test String2";
    $con = mysqli_connect('localhost', 'root', 'root', 'computer science coursework');

    // check for successful connection.
    if (mysqli_connect_errno())
        {
            echo "1: Connection failed"; // Error code #1 - connection failed.
            exit();
        }

    $username = mysqli_escape_string($con, $_POST["username"]);
    $usernameClean = filter_var($username, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
    $password = $_POST["password"];

    if($username != $usernameClean)
    {
        echo "7: Illegal Username, Potential SQL Injection Query. Access Denied.";
        exit();
    }

    // check for if the name already exists.

    $namecheckquery = "SELECT username, salt, hash, score FROM players WHERE username='" . $usernameClean . "';";

    $namecheck = mysqli_query($con, $namecheckquery) or die("2: Name check query failed"); // Error code # 2 - name check query failed.

    if (mysqli_num_rows($namecheck) != 1)
    {
    	echo "5: No User With Your Log In Details Were Found Or More Than One User With Your Log In Details Were Found"; // Error code #5 - other than 1 user found with login details
    	exit();
    }

    // get login info from query
    $existinginfo = mysqli_fetch_assoc($namecheck);
    $salt = $existinginfo["salt"];
    $hash = $existinginfo["hash"];

    $loginhash = crypt($password, $salt);
    if ($hash != $loginhash)
    {
    	echo "6: Incorrect Password"; // error code #6 - password does not hash to match table
    	exit;
    }
    echo "Test String2";
    echo"0\t" . $existinginfo["score"];

?>

 

Link to comment
Share on other sites

On 2/10/2020 at 4:31 PM, requinix said:

Check your error logs in case something happened. Also try setting up and submitting an HTML form to see how the script reacts.

 

Another valuable free tool that can be used to test your serverside api calls is Postman.  Here's an introduction video I scanned that can teach you the basics of using it effectively in a short amount of time.  There are complete courses available on Youtube that cover all aspects of using it, but for your purposes, an investment of 30 minutes or less should have you up and running and testing your login.php script independently from the Unity client code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.