私はAngularJSとPHPのバックエンドで働いている、と私は認証されたユーザのデータを表示しようとしているとのセッションを使用して、私は2ビュー、1「login.htmlと」と他のを持ってする方法'info.html'、2コントローラ(ログイン機能用とユーザーデータ用の2つ)。私は認証フェーズを実装しますが、2番目のステップでは、ユーザーが認証すると、このユーザーのすべての情報が表示される別のビュー(info.html)にリダイレクトされます。angularJS
私はセッションを使用しようとしましたが、動作しません。理由はわかりません。 どのように私は(第2のウェブサービス)
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;
});
}
私はどのようにしてください行うことができ、ユーザー認証されたデータを取得する方法がわかりませんか?あなたはsession
下記のようにログイン機能は、エラーなしで渡されたとき、あなたは仕事に、このために、コントローラで$sessionstorage
サービスを使用する必要がありますを作成する必要があり、事前
Googleで「$ localstorage」または「$ sessionstorage」または「$ cookies」を検索 –