Hello
I’m trying to make a registration/login scene for my game. Unfortunately, the player always will
forwarded even if the username/password or even
both are wrong.
To implement this I followed a tutorial, but it is
not saying whether the same problem exists there or not.
The whole thing is made up of two problems.
Problem 1 (see image) is that the ‘hash’ field in the database has ‘*0’
is entered, but in the tutorial it looked different (similar to the entry in the ‘salt’ field).
In the end, the player is redirected, although
incorrect data may have been entered.
Can someone take a look on my code to tell me where the errors are?
Kind regards
Toby
This could should name the username after login was successful.
public Text playerDisplay;
public void Start()
{
if (DBManager.LoggedIn)
{
playerDisplay.text = "Welcome Commander " + DBManager.username;
}
}
This code contains the LogIn Function/Coroutine
// Login
public void CallLogin()
{
StartCoroutine(LogIn());
}
IEnumerator LogIn()
{
WWWForm loginForm = new WWWForm();
loginForm.AddField("name", username_field.text);
loginForm.AddField("password", password_field.text);
UnityWebRequest wwwlogin = UnityWebRequest.Post("http://localhost/sqlconnect/login.php", loginForm);
yield return wwwlogin.SendWebRequest();
if (wwwlogin.result == UnityWebRequest.Result.Success)
{
DBManager.username = username_field.text;
// DBManager.score = int.Parse(wwwlogin.downloadHandler.text.Split('\t')[1]);
UnityEngine.SceneManagement.SceneManager.LoadScene(1);
}
else
{
Debug.Log("User LogIn failed... " + wwwlogin.error);
}
}
This is the database manager file.
public static class DBManager
{
public static string username;
public static int score;
public static bool LoggedIn { get { return username != null; } }
public static void LogOut()
{
username = null;
}
}
This is the php file for the login process
<?php
$con = mysqli_connect('localhost', 'root', 'root', 'unityaccess');
//check connection
if (mysqli_connect_errno())
{
echo "Error #1"; // error code #1 = Connection failed!
exit();
}
$username = $_POST["name"];
$password = $_POST["password"];
// check if name exists
$namecheckquery = "SELECT username, salt, hash, score FROM players WHERE username='" . $username . "';";
$namecheck = mysqli_query($con, $namecheckquery) or die("Error #2a"); // error code #2a = name check query failed
if (mysqli_num_rows($namecheck) <1)
{
echo "Error #3a"; // error code #3a = name does not exist or something (un)funny is going on (>1)...
exit();
}
if (mysqli_num_rows($namecheck) >1)
{
echo "Error #3b"; // error code #3b = something (un)funny is going on... (too many results?!?)
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 "Error #6"; // errorcode #6 = incorrect password
exit();
}
echo "0\t" . $existingInfo["score"];
?>
9768552–1399608–DBManager.cs (340 Bytes)
9768552–1399611–Register_LogIn_Menu.cs (2.7 KB)
9768552–1399614–MainMenuScript.cs (773 Bytes)