即使AngularJS和PHP的凭据不正确,登录仍然是直接的
Login still directs even if incorrect credential with AngularJS and PHP
PHP代码需要从表中提取数据,将其解码为JSON,然后将其发送到AngularJS。
如果登录凭据正确,Angular JS会重定向。
但是,即使凭据不正确,用户仍然会被重定向(true语句总是通过)。
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控制器:
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");
});
}
});
HTML表单代码
<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>
您必须将if-else
条件添加到success
回调中,因为即使您从PHP代码发送false
,也总是发送200响应
$scope.checkCredentials = function (credentials) {
$http.post('model/getAdmin.php',credentials).success(function(data){
console.log("true", data);
if (data == true) { // Or in whatever form you are receiving true/false
$location.path("/memberList");
} else {
$log.error(err);
console.log("false");
}
})
}
如果你想让你的旧代码工作,即与success
&在Angular中的error
回调,然后你需要从你的PHP代码中发送一个非200响应代码和false
,比如:
if($count == 1)
{
echo true;
}
else
{
http_response_code(406); // not acceptable response code
echo false;
}
(推荐第二种解决方案)
我从未使用过PHP,所以请确保http_response_code(406);
的编写是正确的。
相关文章:
- 将脚本缓存到本地存储的basket.js概念仍然是一个好主意吗
- 浏览器仍然是单线程的吗
- 重新审视网站优化的经验法则:JavaScript在现代浏览器中仍然是必不可少的
- 调整图像大小以使其完美地适合幻灯片,问题仍然是用 for 循环将 CSS 代码替换为我的图像
- 为什么“使用严格”仍然是一个字符串字面
- 不可设置和不可写属性仍然是可变的
- 速度.js重置 {bottom, top} 为“auto” 不起作用,它仍然是“0px”
- 持续时间为零的css转换是否仍然是硬件加速的
- getElementById仍然是“;“安全”;DOM操作方法
- 即使AngularJS和PHP的凭据不正确,登录仍然是直接的
- 在Twitter Bootstrap 3按钮(复选框)仍然是灰色后,它是未检查的
- PHP飞到购物车效果jquery ajax /我想提交表单,但仍然是相同的页面
- Javascript:虽然变量是在全局作用域中声明的,但它在函数内部仍然是未定义的
- 为什么我的任何时间DateTimePicker jQuery插件仍然是一个常见的文本框
- 弹出窗口浏览器仍然是Windows主页的一部分
- Javascript的全局值在试图通过函数改变后仍然是NULL
- 我仍然是JS的新手,不能看到我在这个基本计算脚本上的错误
- 角,Loopback:用户.登录不是一个功能
- 在元素上使用Object.defineProperty.数据集,但属性仍然是可更改的
- SessionStorage似乎仍然是空的