我正在使用angularJS和php后端进行应用程序,我需要知道2个角色,客户端和管理员,每个角色都有另一个完全不同的空间。所以我认为认证步骤中的一个简单的“if”条件可以解决问题。
要理解代码:
通过检查来自两个表的用户的电子邮件和密码来完成身份验证:一个管理表和一个客户端表,每个表包含“admin”角色列,用于“客户端”角色,用于“客户端” “表。
问题:
如何将if条件放在select的结果上,然后如果角色是admin:它将进入管理空间,如果是客户端,则相反。
我尝试做的事情:
的login.php
<?php
$data = json_decode(file_get_contents("php://input"));
$connect = mysqli_connect("localhost", "root", "", "test");
if(count($data) > 0)
{
$Email=mysqli_real_escape_string($connect, $data->Email);
$mdp=mysqli_real_escape_string($connect, $data->mdp);
$query = 'SELECT * FROM `client`,`admin` WHERE (EmailClient = "'.$Email.'" AND mdp= "'.$mdp.'") OR (EmailaAdmin = "'.$Email.'" AND MdpAdmin= "'.$mdp.'")';
$q = mysqli_query($connect , $query);
if(mysqli_num_rows($q) > 0 )
{
$_SESSION["logged_in"] = true;
$_SESSION["naam"] = $Email;
$result['code'] = 200;
$result['message'] ='Logged In';
}
else
{
$result['code'] = 603;
$result['message'] ='The username or password are incorrect!';
}
$resultstring = json_encode($result);
$resultstring = str_replace("null",'""',$resultstring);
echo $resultstring;
}
?>
app.js
app.controller('loginCtrl', function($scope, $location,$state,$http,$window){
$scope.submit = function()
{
data = {
'Email' : $scope.Email,
'mdp' : $scope.mdp
};
$http.post('http://localhost/test/login.php', data)
.success(function(data, status, headers, config,result)
{
console.log(data);
if(data.code == 200){
if(data.role=client){
$state.go('client');
}
else{
$state.go('admin');
}
}
else{
alert('incorrect');
}
})
.error(function(data, status, headers, config, rsult)
{
console.log('error');
});
}
});
提前致谢
答案 0 :(得分:1)
一种简单的方法可能是
<?php
$data = json_decode(file_get_contents("php://input"));
$connect = mysqli_connect("localhost", "root", "", "test");
if(count($data) > 0)
{
$Email = mysqli_real_escape_string($connect, $data->Email);
$mdp = mysqli_real_escape_string($connect, $data->mdp);
$query = 'SELECT * FROM `admin` WHERE (EmailClient = "'.$Email.'" AND mdp= "'.$mdp.'") OR (EmailaAdmin = "'.$Email.'" AND MdpAdmin= "'.$mdp.'")';
$q = mysqli_query($connect , $query);
if(mysqli_num_rows($q) > 0 )
{
$_SESSION["logged_in"] = true;
$_SESSION["naam"] = $Email;
$result['code'] = 200;
$result['message'] ='Logged In';
$result['role'] ='admin';
$resultstring = json_encode($result);
$resultstring = str_replace("null",'""',$resultstring);
echo $resultstring;
exit;
}
$query2 = 'SELECT * FROM `client` WHERE (EmailClient = "'.$Email.'" AND mdp= "'.$mdp.'") OR (EmailaAdmin = "'.$Email.'" AND MdpAdmin= "'.$mdp.'")';
$q2 = mysqli_query($connect , $query2);
if(mysqli_num_rows($q2) > 0 )
{
$_SESSION["logged_in"] = true;
$_SESSION["naam"] = $Email;
$result['code'] = 200;
$result['message'] ='Logged In';
$result['role'] ='client';
$resultstring = json_encode($result);
$resultstring = str_replace("null",'""',$resultstring);
echo $resultstring;
exit;
}
$result['code'] = 603;
$result['message'] ='The username or password are incorrect!';
$resultstring = json_encode($result);
$resultstring = str_replace("null",'""',$resultstring);
echo $resultstring;
exit;
}
通过if(data.role=='client'){ //client
或if(data.role=='admin'){ //admin