简体   繁体   中英

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

I am making a login with ajax where the data is stored in a text file and for some reason my login doesn´t work and it consols login failed as well as the php echos the login: error. This is my javascript code bit:

//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 );
    }   

And here is the 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;
    }
}


?>

Here your result will match only with first element. SO you need to use flag variable to match data

$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;

Use this:

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM