dqqfuth6736 2018-08-25 18:57
浏览 102

等待mysql服务器的响应很简单

I'm new to programming and wrote a basic login script (or rather watched a tutorial), but my output in Unity is at first "User nicht gefunden" and about one second later "User gefunden".

My question: Can I somehow wait for a response from the mysql server in a easy way and still recognize a missing connection?

Unityscript:

public string inputusername;
public string inputpassword;

public string URL = "http://localhost/login.php";


void Start () {

}


void Update () {
    if (Input.GetKeyDown(KeyCode.L))
    {
       StartCoroutine(LoginToDB(inputusername, inputpassword));
    }
}

IEnumerator LoginToDB(string username, string password)
{
    WWWForm form = new WWWForm();
    form.AddField("usernamePost", username);
    form.AddField("passwordPost", password);

    WWW www = new WWW(URL, form);
    yield return www;


    if(www.text == "wrong")
    {
        Debug.Log("User nicht gefunden");
    }
    if(www.text == "correct")
    {
        Debug.Log("User gefunden");
    }


}

Phpscript:

$servername = "localhost";
$username = "root";
$password = "********";
$dbName = "test";

$user_username = $_POST["usernamePost"];
$user_password = $_POST["passwordPost"];

 $conn = new mysqli($servername, $username, $password, $dbName);

if(!$conn){
    die("Verbindung Fehlgeschlagen!". mysqli_connect_error());
}

$sql = "SELECT password FROM users WHERE username = '".$user_username."' ";
$result = mysqli_query($conn, $sql);



if(mysqli_num_rows($result) > 0){

    while($row = mysqli_fetch_assoc($result)){

         if($row['password'] == $user_password){
             echo "correct";
         } 

         else{

                echo "wrong";
         }

    }

}else {
    echo"not here";

}

Sorry in advance for the probably simple question.

  • 写回答

1条回答 默认 最新

  • dongwu1410 2018-08-26 01:44
    关注

    I see three issues here:

    while($row = mysqli_fetch_assoc($result)){
    
         if($row['password'] == $user_password){
             echo "correct";
         } 
    
         else{
    
                echo "wrong";
         }
    
    }
    
    1. You aren't checking that the passed in user matches the user in the database. Multiple users with the same password could log in add each other!
    2. You are returning multiple responses, this is why you see "wrong password" initially
    3. You aren't salting and hashing your passwords. This is inherently insecure.
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建