如何使用PHP和AngularJS选择经过身份验证的用户数据

时间:2017-07-28 11:20:24

标签: php angularjs authentication

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

问题:

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

的login.php

<?php  
session_start();
$_SESSION['token'] = true;
$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` WHERE (EmailClient = "'.$Email.'" AND   mdp= "'.$mdp.'")';

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

if(mysqli_num_rows($q) > 0 )
  { 
       $token = md5($Email.time()."51395+81519851");
       $query = "UPDATE client SET token = '".$token."' WHERE EmailClient = '".$Email."'";
       mysqli_query($connect , $query);
       $_SESSION["logged_in"] = true; 
       $_SESSION["token"] = $token; 
       $_SESSION["Email"] = $Email; 
       $result['token'] = $token;           
       $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的

<?php  
session_start();
 $connect = mysqli_connect("localhost", "root", "", "test");  

 $output = array();  
 $query = "SELECT Name,Adresse FROM client WHERE token = '".$_SESSION['token']."'";
 $result = mysqli_query($connect, $query);  
 if(mysqli_num_rows($result) > 0)  
 {  
      while($row = mysqli_fetch_array($result))  
      {  
           $output[] = $row;  
      }  
      echo json_encode($output);  
 }  

 ?>

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

提前致谢

1 个答案:

答案 0 :(得分:0)

在登录控制器中:

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

    $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);
            $cookies.putObject('AuthenticateData', data);
            $state.go('info');

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

});

在信息控制器中:

app.controller('infoCtrl', function($scope, $http,$state,$filter, $cookies){
//use something like this
   var authenticateData =  cookies.getObject("AuthenticateData");
});

这只是一个展示Cookie如何运作的示例。你可以在其中解决。