【问题标题】:how to get authenticated user data如何获取经过身份验证的用户数据
【发布时间】:2017-11-29 10:02:57
【问题描述】:

我有一个问题我已经尝试解决了很长时间。我已经阅读了有关 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');   
        });
    }

});

信息控制:

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 ???" ;
....

我不知道如何获取用户认证数据,请问我该怎么做?

提前致谢

【问题讨论】:

    标签: php angularjs ionic-framework


    【解决方案1】:

    在您的登录控制器中使用$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
    }); 
    

    【讨论】:

    • 谢谢,但是如何在信息控制器和 Web 服务中检索经过身份验证的用户数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2018-11-26
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多