如何用php

时间:2017-07-18 10:48:43

标签: php angularjs session

我正在使用PHP后端开发AngularJs应用程序,我实现了身份验证步骤,现在我正在尝试在身份验证后在另一个视图中显示经过身份验证的用户的数据。

我使用会话令牌在其他视图中显示用户数据但是即使我在一些关于会话和令牌的教程中读到的内容超过两周,我仍然无法显示用户数据。我在display.php文件中收到此错误:

  

未定义索引:令牌

我的申请无法进展。我正在开展会议。

的login.php

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

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

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

?>

Display.php的

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

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

 ?>

0 个答案:

没有答案