2016-03-06 78 views
-1

當我輸入了正確的憑據結果是{"success":1,"message":"Login successful!"}ANDROID&PHP |登錄

,當我故意輸入的密碼不正確,這是結果,我不明白這個登錄腳本

<?php 
require("dbConnect.php"); 

if (!empty($_POST)) { 
    $query = "SELECT * FROM tb_user WHERE instructor_id = :instructor_id"; 

    $query_params = array(
     ':instructor_id' => $_POST['instructor_id'] 
    ); 

    try { 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 
    } 
    catch (PDOException $ex) { 

     $response["success"] = 0; 
     $response["message"] = "Database Error1. Please Try Again!"; 
     die(json_encode($response)); 
    } 

     $row = $stmt->fetch(); 
    if ($row) { 
     //if we encrypted the password, we would unencrypt it here, but in our case we just 
     //compare the two passwords 
     $password = $_POST['password']; 
     if (md5($password) == $row['password']) { 
      $response["success"] = 1; 
      $response["message"] = "Login successful!"; 
      die(json_encode($response)); 
     }else { 
     $response["success"] = 0; 
     $response["message"] = "Invalid Credentials!"; 
     die(json_encode($response)); 
     } 
    } 

} else { 
?> 
     <h1>Login</h1> 
     <form action="login.php" method="post"> 
      Username:<br /> 
      <input type="text" name="instructor_id" placeholder="instructor_id" /> 
      <br /><br /> 
      Password:<br /> 
      <input type="password" name="password" placeholder="password" value="" /> 
      <br /><br /> 
      <input type="submit" value="Login" /> 
     </form> 
    <?php 
} 
?> 

{"success":0,"message":"Invalid Credentials!"}

但問題是當我輸入兩個錯誤的用戶名和傳遞沒有JSON顯示結果,我期待的結果仍然應該這樣{"success":0,"message":"Invalid Credentials!"},因爲我輸入了錯誤的登錄憑據

回答

2

試試這個:

if ($row) { 
    //compare the two passwords 
    $password = $_POST['password']; 

    if (md5($password) == $row['password']) { 
     $response["success"] = 1; 
     $response["message"] = "Login successful!"; 
     die(json_encode($response)); 
    } else { 
     $response["success"] = 0; 
     $response["message"] = "Invalid Credentials!"; 
     die(json_encode($response)); 
    } 

} else { 
    $response["success"] = 0; 
    $response["message"] = "Invalid Credentials!"; 
    die(json_encode($response)); 
} 
+1

謝謝先生,它可以幫助我很多 – CallMeJeo