如何获取经过身份验证的用户数据

时间:2017-06-26 14:18:17

标签: php angularjs ionic-framework

我有一个问题,我一直试图解决很长一段时间。我已经阅读了有关PHP会话的课程,我有建议使用localstorage但无济于事

问题:

我正在使用angularJS和PHP后端,我有2个视图,一个' login.html'和另一个' info.html',2个控制器(一个用于登录功能,另一个用于选择用户的数据)。我设法进行了身份验证阶段,但是对于第二步,我希望在用户进行身份验证时,它会被重定向到其他视图(info.html),其中将显示该用户的所有信息。 如何存储登录功能中的数据并在第二个控制器中使用

login.php

 <?php  
session_start();
$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);
$password=mysqli_real_escape_string($connect, $data->password);

$query = 'SELECT * FROM `client` WHERE (EmailClient = "'.$Email.'" AND   password= "'.$password.'")';

$q = mysqli_query($connect , $query);

if(mysqli_num_rows($q) > 0 )
  { 
       $_SESSION["logged_in"] = true; 
       $_SESSION["naam"] = $Email; 
       $result['email'] =$Email;
       $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;
}

?>

loginCtrl

app.controller('loginCtrl', function($scope, $location,$state,$http,$window){

    $scope.submit = function()
    {
        data = {
            'Email' : $scope.Email,
            'password' : $scope.password
    };

        $http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config,result)
        {
            console.log(data);
            $state.go('info');

             }
        })
        .error(function(data, status, headers, config, result)
        {      
            console.log('error');   
        });
    }

});

infoCtrl:

app.controller('infoCtrl', function($scope, $http,$state,$filter){

    $scope.loadColis = function(){ 
              $http.get("http://localhost/deb/info.php")  
           .success(function(data){  
                $scope.names = data;  
           });
      } 

info.php的

...
$query = "SELECT * FROM client where ???" ;
....

我不知道如何获取用户身份验证的数据,我该怎么办?

提前致谢

1 个答案:

答案 0 :(得分:0)

在登录控制器中使用$emit

app.controller('loginCtrl', function($scope, $location,$state,$http,$window){

    $scope.submit = function()
    {
        data = {
            'Email' : $scope.Email,
            'password' : $scope.password
    };

        $http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config,result)
        {
           //If the user is an authenticated user then make Api call to get authenticated user data using $http.get and pass the data to $emit

            $scope.$emit('authenticatedUserData', {data: data});                 
            $state.go('info');

             }
        })
        .error(function(data, status, headers, config, result)
        {      
            console.log('error');   
        });
    }

});

在您的信息控制器中使用$on

app.controller('infoCtrl', function($scope, $http,$state,$filter){
$scope.$on('authenticatedUserData', function (event, data) {
  // Logic goes here
});