我正在使用AngularJS和PHP后端,我正在尝试显示经过身份验证的用户的数据,我有2个视图,一个是'login.html',另一个是'info.html',2个控制器(一个用于登录功能,另一个用于选择用户的数据)。我实现了身份验证阶段,但对于第二步,我希望当用户进行身份验证时,它将被重定向到其他视图(info.html),其中将显示该用户的所有信息。
我尝试使用会话,但它不起作用,我不知道为什么。如何从登录功能存储用户数据并在第二个控制器(第二个Web服务)中使用它?
的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 )
{
$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['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", "", "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);
}
?>