簡體   English   中英

使用 AJAX 登錄,其中數據位於文本文件中

[英]Login with AJAX, where data is located in a text file

我正在使用 ajax 進行登錄,其中數據存儲在文本文件中,由於某種原因,我的登錄不起作用,它使登錄失敗以及 php 回顯登錄:錯誤。 這是我的 javascript 代碼位:

//Logs in the user
  btnLoginForm.addEventListener( "click", loginUser);

    function loginUser() {
        var ajax = new XMLHttpRequest();
        ajax.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            ajUserDataFromServer = JSON.parse( this.responseText );

            console.log("The return from the server = " +   ajUserDataFromServer);

            if( ajUserDataFromServer.login == "ok" ){

                console.log("WELCOME");
              } 

            else 
                {

                console.log("LOGIN FAIL - TRY AGAIN");  

                }
            }
        }
      ajax.open( "POST", "api_login_users_wm.php" , true );
      var jFrmLogin = new FormData( frmLogin );
      ajax.send( jFrmLogin );
    }   

這是php:

session_start();

// Load all the users and decode them to an array
$sajUsers = file_get_contents('webshop_mandatory_users.txt');
$ajUsers = json_decode($sajUsers);//turns the string into an array.

// Data comes from the browser
$sEmail = $_POST['txtEmail'];
$sPassword  = $_POST['txtPassword'];


// begin looping through the array.
for( $i = 0; $i < count($ajUsers) ; $i++ ) {

    //check if the data from the frontend matches any date in the backend - Check each one, one by one.
    if ($sEmail == $ajUsers[$i]->email && $sPassword == $ajUsers[$i]->password) {     //checks if the value of the username is equal to the value in the array.
        echo $sjResponse = '{"login":"ok"}';
        exit; //end the if statement and exit if it works.
    } else {
        echo $sjResponse = '{"login":"error"}'; // it didnt work.
        exit;
    }
}


?>

在這里,您的結果將僅與第一個元素匹配。 所以你需要使用標志變量來匹配數據

$match_found = false;
for( $i = 0; $i < count($ajUsers) ; $i++ ) {

    //check if the data from the frontend matches any date in the backend - Check each one, one by one.
    if ($sEmail == $ajUsers[$i]->email && $sPassword == $ajUsers[$i]->password) {     //checks if the value of the username is equal to the value in the array.
        $match_found = true;
        break;
    } 
}

if($match_found )
{
    echo '{"login":"ok"}';
}
else
{
    echo '{"login":"ok"}';
}
exit;

用這個:

$flagLogin = false;
// begin looping through the array.
for( $i = 0; $i < count($ajUsers) ; $i++ ) {
    //check if the data from the frontend matches any date in the backend - Check each one, one by one.
    if ($sEmail == $ajUsers[$i]->email && $sPassword == $ajUsers[$i]->password) { //checks if the value of the username is equal to the value in the array.
        $flagLogin = true;
        break;
    }
}
if($flagLogin) {
    echo $sjResponse = '{"login":"ok"}';
    exit; //end the if statement and exit if it works.
} else {
    echo $sjResponse = '{"login":"error"}'; // it didnt work.
    exit;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM