Kod PHP musi pobrać dane z tabeli, dekodować je jako JSON, a następnie wysłać do AngularJS.Login nadal kieruje, nawet jeśli nieprawidłowe poświadczenia z AngularJS i PHP
Angular JS przekierowuje, jeśli dane logowania są poprawne.
Jednak nawet jeśli poświadczenia są niepoprawne, użytkownik jest nadal przekierowywany (prawdziwe polecenie zawsze przechodzi).
Kod PHP:
<?php
$data = json_decode(file_get_contents("php://input"));
$username = $data->username;
$password = $data->password;
/*
* Collect all Details from Angular HTTP Request.
*/ require_once("connection.php");
//must read from table
$connection = connectToMySQL();
//complete from here
$query = "SELECT count(*) FROM tbl_admin WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($connection ,$query);
$row = mysqli_fetch_row($result);
$count = $row[0];
if($count == 1)
{
echo true;
}
else
{
echo false;
}
?>
angularjs Kontroler:
app.controller('loginCtrl', function ($scope, $http, $location) {
/*
* This method will be called on click event of button.
* Here we will read the email and password value and call our PHP file.
*/
$scope.checkCredentials = function (credentials) {
$http.post('model/getAdmin.php',credentials).success(function(data){
console.log("true");
$location.path("/memberList");
})
.error(function(err){
$log.error(err);
console.log("false");
});
}
});
kod HTML Formularz
<form class="form-group" id="customForms" ng-controller="loginCtrl">
<label> Username </label>
<input id="customFormsInput" class="form-control" ng-model="credentials.username" type="text" placeholder="Username goes here" required/>
<br>
<label> Password </label>
<input id="customFormsInput" class="form-control" ng-model="credentials.password" type="password" placeholder="Password goes here" required/>
<br> <br>
<button class="btn btn-primary" type="submit" ng-click="checkCredentials(credentials)"> Submit </button>
<br>
{{responseMessage}}
<!-- Shows message depending on login type -->
</form>
Wypróbuj 'console.log (dane)' i zobacz odpowiedź serwerów. –
Zmieniłem echo ze strony php, a teraz 'console.log (dane)' pokazało mi, że od strony PHP walidacja jest dobra. (Zwraca PHP True i PHP False). Jednak kontrola przechodzi tylko przez prawdziwe stwierdzenie. – hurkaperpa
'$ http.post' rozwiązuje, nawet jeśli serwer odpowie" false ". Zamiast tego serwer może odpowiedzieć z pewnymi informacjami w JSON, tak że twoja aplikacja wie DLACZEGO sprawdzanie poświadczeń nie powiodło się. –