我有一個問題,我一直試圖解決很長一段時間。我看了一下PHP會話課程,我建議使用localStorage的,但無濟於事如何使用PHP和AngularJS選擇經過身份驗證的用戶數據
問題:
林與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);
}
?>
我不知道如何獲取用戶認證的數據,我該怎麼辦嗎?
在此先感謝
你好。請查看http://bobby-tables.com並瞭解SQL注入以及如何防止它們。其實你的代碼根本不安全。 – Twinfriends
使用會話存儲或cookie在整個應用程序中訪問已驗證的數據。 – Indhu
@Indhu,你可以給我一個想法,我該怎麼做,請! – Mimoun