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.
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!