Login cookies
I have created a simple login with Unit, php and mysql for Android devices. However I am facing difficulties to understand how cookies work and are the cookies the right way to persist the authentication next time when the game is opened. As far as I have seen I can create cookies in two ways:
Create cookies in Unity with responseHeaders
Create cookies in php with setcookies method and then retrieve those values in Unity*
Or am I wrong and those two options need to be combined to create cookies?
Below is my existing code. Can someone help me to modify the code so I can manage cookies?
Unity
IEnumerator LoginPlayer() {
WWWForm form = new WWWForm();
form.AddField("name", loginNameField.text);
form.AddField("password", loginPasswordField.text);
WWW www = new WWW("http://.../login.php", form);
yield return www;
if (www.text[0] == '0') {
...
LoginSucessfully();
}
else {
Debug.Log("User login failed. Error #" + www.text);
}
}
PHP
...
$username = $_POST["name"];
$password = $_POST["password"];
//check if name exists
...
//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: Incorect Password";
exit();
}
...
Answer by DownER149 · Dec 12, 2018 at 09:04 AM
change
$username = $_POST["name"];
$password = $_POST["password"];
to
$_SESSION[username] = $_POST["name"];
$_SESSION[password] = $_POST["password"];
session is like a cookie but is removed when the session ends. probably better for login credentials. but if you prefer cookies i don't see where you actually set the cookie in the code.
Answer by electro_unity · Feb 19, 2019 at 09:38 PM
Did you find the answer? I'm facing the same problem, I don't know how, where or which information do I have to store in order to automatically open the player session next time he enters the game.