简体   繁体   中英

Incorrect password when login in using php hash and salt

Im doing a project where you can create an accoun, log in an save user data in unity using php and a mysql server. I did all the registering process and it worked but I cant get the login working. Im doing this following the boardtobits tutorial.

Doing some research I have read that the hash generated when created the account and the loginhash are not the same, but why?

PHP CODE - (for login)

<?php

 $user = 'root';
 $password = 'root';
 $db = 'hostalvillegadata';
 $host = 'localhost';
 $port = 8889;

 $link = mysqli_init();
 $success = mysqli_real_connect(
 $link, 
 $host, 
 $user, 
 $password, 
 $db,
 $port);


//check that connection happened
if (mysqli_connect_errno()) 
{
echo "1: Connection failed"; //error code #1 = connection failed
exit();
}

$username = $_POST["name"];
$password = $_POST["password"];

//check if name exists
$namecheckquery = "SELECT username, salt, hash, habitacionesreservadas 
FROM hostalvillegatable WHERE username='" .$username. "';";

$namecheck = mysqli_query($link, $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 name, or more than one"; //error code #5 - number 
 of names matching != 1
 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 "0\t" . $existinginfo["habitacionesreservadas"];

   ?>

C# CODE-(Login)

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

public Button submitButton;

public void CallLogin()
{
    StartCoroutine(Login());
}

IEnumerator Login()
{
    WWWForm form = new WWWForm();
    form.AddField("name", nameField.text);
    form.AddField("password", nameField.text);

    UnityWebRequest www = UnityWebRequest.Post("http://localhost:8080/sqlconnect/login.php", form);
    yield return www.SendWebRequest();

    if (www.downloadHandler.text[0] == '0')
    {
        DataBaseManager.username = nameField.text;
        DataBaseManager.habitacionesreservadas = int.Parse(www.downloadHandler.text.Split('\t')[1]);
    }
    else
    {
        Debug.Log("User login failed. Error #" + www.downloadHandler.text);
    }

}

public void VerifyInputs()
{
    submitButton.interactable = (nameField.text.Length >= 5 && passwordField.text.Length >= 5);
}

}

C# CODE (DataBaseManager)

public static class DataBaseManager
{
public static string username;
public static int habitacionesreservadas;

public static bool LoggedIn {get { return username != null; } }

public static void LogOut()
{
    username = null;
}

It should work and login the user but instead it tells me the password is incorrect, I have created several account with same password an no changes.

Any idea how to solve this?

this line

$existinginfo = msqli_fetch_assoc($namecheck);

must be

$existinginfo = mysqli_fetch_assoc($namecheck);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM