首页 > 解决方案 > 使用php hash和salt登录时密码错误

问题描述

我正在做一个项目,您可以在其中创建一个帐户,使用 php 和 mysql 服务器统一登录并保存用户数据。我完成了所有注册过程并且它有效,但我无法让登录工作。我按照 boardtobits 教程执行此操作。

做一些研究,我读到创建帐户时生成的哈希和登录哈希不一样,但为什么呢?

PHP 代码 - (用于登录)

<?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-(登录)

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# 代码(数据库管理器)

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;
}

它应该可以工作并登录用户,但它告诉我密码不正确,我创建了几个具有相同密码的帐户并且没有更改。

知道如何解决这个问题吗?

标签: c#phpmysqlunity3dserver

解决方案


这条线

$existinginfo = msqli_fetch_assoc($namecheck);

一定是

$existinginfo = mysqli_fetch_assoc($namecheck);


推荐阅读