2017-07-12 70 views
-1

我與AngularJS和PHP後臺工作,而我試圖顯示認證用戶的數據會話,和我有2次,一個「的login.html」;另'info.html',2個控制器(一個用於登錄功能,另一個用於選擇用戶的數據)。我實現了身份驗證階段,但對於第二步,我希望當用戶進行身份驗證時,它將被重定向到其他視圖(info.html),其中將顯示此用戶的所有信息。如何使用與angularJS

我試圖使用會話,但它不工作,我不知道爲什麼。 如何從登錄功能存儲用戶的數據,並在第二控制器使用它(第二Web服務)

的login.php

 <?php 
    session_start(); 
    $data = json_decode(file_get_contents("php://input")); 

    $connect = mysqli_connect("localhost", "root", "", "user"); 

    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; 
; 
      $result['message'] ='Logged In'; 
      $result['email'] =$Email; 
      $result['token'] = $token; 

      $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; 
    } 

    ?> 

info.php的

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

$output = array(); 
$query = "SELECT FirstName,FamilyName,EmailClient 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); 
} 

?> 

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; 
      }); 
     } 

我不知道如何獲取用戶認證的數據,我該怎麼辦嗎?

在此先感謝

+0

谷歌搜索'$ localstorage'或'$ sessionstorage'或'$ cookies' –

回答

0

你需要創建一個session當登錄功能沒有任何錯誤傳遞,如下面給出的,你必須在控制器使用$sessionstorage服務這個工作。

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

     //Here create a session, so I am creating a "user" session with its details 
     $rootScope.globals = { 
      currentUser: { 
       firstname: data.firstname, 
       lastname: data.lastname, 
      } 
     }; 

     $sessionStorage.user = $rootScope.globals; 

     $state.go('info'); 

    } 
    }) 

然後你可以使用$sessionStorage.user.currentUserinfo頁面的用戶的詳細信息,並再次將需要$sessionStorage服務在其控制器也。

+0

謝謝,但是我怎樣才能在php文件中選擇當前用戶的數據 – Touria

+0

數據將存儲在這裏$ sessionStorage.user.currentUser ,我不知道你如何訪問這個到PHP –