2017-05-24 141 views
1

我有一個項目的登錄信息,我試圖弄清楚。我已經通過POST獲得了一個值(通過AJAX調用),我已經檢查過輸入的用戶名是否存在並且直到那裏,它運行良好。但知道我想檢查密碼是否對該用戶名有效。以下是PHP代碼:PHP - MYSQL - 如果用戶名存在,請檢查特定用戶名的密碼

<?php 

    //File with the conection data 
    include_once "../conexion.php"; 

    //Initialization 
    $user = ""; 
    $password = ""; 
    $errors = ""; 
    $result = ""; 
    $result2 = ""; 

    //Some validations (I've edited a little to make it shorter) 
    if((isset($_POST['user'])) && (!empty($_POST['user']))){ 
     $user = $_POST['user']; 
    }else{ 
     $errors .= "blablablah"; 
    } 

    if((isset($_POST['password'])) && (!empty($_POST['password']))){ 
      $password = $_POST['password']; 
     }else{ 
      $errors .= "blablabla"; 
     } 

    //I make the query 
    $sql = "SELECT user FROM users WHERE user = ?"; 

    //I prepare the query 
    if($stmt = $con->prepare($sql)){ 

     $stmt->bind_param('s', $user); 
     $result = $stmt->execute(); 

    } 

    /* UP TO HERE, if I check that $result is true and echo "User exists" or something like that, IT WORKS, AS THE USER EXISTS */ 

    /* BUT now I want to check the PASSWORD, given that the user exists */ 

     if($result){ 

      //I make the query 
      $sql2 = "SELECT user, password FROM users WHERE user = ? AND password = ?"; 

      //I prepare the query 
      if($stmt2 = $con->prepare($sql2)){ 

       $stmt2->bind_param('ss', $user, $password); 
       $result2 = $stmt2->execute(); 


       if($result2){ 
        echo "ENTERED"; 
       }else{ 
        echo "PASSWORD OR USER INCORRECT"; 
       } 
      } 

     } 

?> 

我使用在AJAX調用成功函數的回波的結果,下面是該代碼(有一個onClick事件(的onClick =「登錄()」)中形式的按鈕,validationLogin()對字段的所有的valitations - >所有工作正常):

function login(){ 

if(validationLogin()){ 
     $.ajax({ 

       url: "http://localhost/myProject/extras/Login.php", 
       type: "POST", 
       data: {"user": user, 
         "password": password, 
         }, 
       dataType: "html", 
       cache: false, 
       beforeSend: function() {  
        console.log("Processing..."); 
       }, 
       success: 
         function(data){ 

         alert(data); 
         console.log(data); 

        } 

    }); 

}else{ 
    //alert("Incorrect fields"); 
} 

} 

這返回空,我提醒數據只是爲了檢查一下,有...警報是空的,不明白爲什麼:/

我試過這個想法 - >PHP mySQL check if username and password are in the database,但在這種情況下,它口口聲聲說這是不正確的:/

的幾個注意事項:

  • 我知道密碼應該被加密,可能會稍後使用MD5。
  • 通過在PHP文件中使用回聲和JS文件中的alert(data)/ console.log(data),我只想檢查是否有效,以便繼續。也許還有其他辦法,更好的方式做這一切,我知道,但我喜歡一點一點地去
  • 我真的想明白我的代碼,然後會改善它,我真的想了解如何以及爲什麼它的功能或不
  • 我想繼續使用準備好的語句

感謝大家提前! :)

+1

**從不**存儲純文本密碼。你應該使用['password_hash()'](http://us3.php.net/manual/en/function.password-hash.php)和['password_verify()'](http://us3.php。 net/manual/en/function.password-verify.php)。如果您使用的是5.5之前的PHP版本,請不要**使用MD5或SHA1來散列密碼。相反,您可以使用[此兼容包](https://github.com/ircmaxell/password_compat)。 –

+0

@AlexHowansky感謝您的支持,在我的帖子結尾處我已經提到了這件事。 – xragdollqueen

+0

*「我試過這個想法 - > PHP mySQL檢查用戶名和密碼是否在數據庫中,但在這種情況下,它一直說這是不正確的」* - 我已經在那裏接受了答案(可以追溯到2014年) ,所以不管OP的問題代碼/答案有什麼區別,那麼別的東西都會讓你失望。 –

回答

2

你應該試試這個:

$sql = "SELECT user,password FROM users WHERE user = ?"; 

//I prepare the query 
if($stmt = $con->prepare($sql)){ 

    $stmt->bind_param('s', $user); 
    $result = $stmt->execute(); 
    if ($result) { 
     $stmt->bind_result($user_name,$password_db);  
    } else { 
     $user_name=""; 
     $password_db=""; 
    } 
    // Check Password 
    if ($password==$password_db){ 
     /// PASSWORD IS OK 
    } 
} 

現在,你必須在$ USER_NAME用戶和$密碼的密碼(如果存在的話),所以你不需要第二個SQL語句。在PHP函數中,您可以使用:

data: {"user": <?php echo $user_name ?>, 
      "password": <?php echo $password_db ?> , 
      }, 
+0

這不會比較密碼值 – Martin

+0

@Martin答案對某個點是正確的;只是不完全。 –

+0

@ Fred-ii-是的,我開始寫一個記錄的答案(如果使用'empty'則不需要'isset'),這會產生類似的影響。我應該把時間花在SO上,而不是SE上:Code Review。修復其他民族的東西(免費': - /') – Martin