Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用angular 7的API调用不';不读取PHP会话变量_Php_Angular - Fatal编程技术网

使用angular 7的API调用不';不读取PHP会话变量

使用angular 7的API调用不';不读取PHP会话变量,php,angular,Php,Angular,我需要创建简单的登录和使用会话。 为此,我有两个php文件和angular文件 这是PHP文件1 session_start(); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type'); $postdata = f

我需要创建简单的登录和使用会话。 为此,我有两个php文件和angular文件

这是PHP文件1

session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');


$postdata = file_get_contents("php://input");

if(isset($postdata) && !empty($postdata)) {

    $request = json_decode($postdata);
    $username=trim($request->username);
    $password=trim($request->password);

  if($username == 'admin' && $password == 'admin') {
    $_SESSION['user'] = 'admin';
    ?>
{
  "success": true,
  "secret": "This is the secret no one knows but the admin"
}
    <?php
  } else {
    ?>
{
  "success": false,
  "message": "Invalid credentials"
}
    <?php
  }
} else {
  //var_dump($_POST)
  ?>
{
  "success": false,
  "message": "Only POST access accepted"
}
  <?php


}

?>
但用户会话仍然不可用。。我需要帮助来理解原因。 请检查图像。 第一个post请求成功,但第二个get请求失败。 它说我使用的会话变量(用户)不可用。
但该变量已由以前的请求设置


感谢您

您正在使用两个不同的服务器localhost:80(适用于PHP)和localhost:4200(适用于angular),因此您对会话的请求是localhost:4200。此请求无法访问PHP会话。
因此,请为PHP端请求设置角度标题。

您应该将_this.user.getSomeData()放在第一个订阅中。在这种情况下,它确保在从这个.auth.getUserDetails('admin','admin')创建会话之后调用。我也尝试过这个方法,但它的作用仍然好像没有设置$\u会话['user']。this.auth.getUserDetails('admin','admin').subscribe(数据=>{console.log('success',data);if(data.success){let_this=this;_this.user.getSomeData().subscribe(data1=>{console.log(data1);})else{window alert(“无效”)},error=>{console.log('failed',error);})似乎angular没有获取PHP会话ID。这将是它的原因。当您使用PHP$\u会话时,它将向客户端发送一个cookie,但如果不是这样,则不会保存会话密钥。
    <?php
    session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

    $user = $_SESSION['user'];

             echo '{
                "message": "'.$user.'",
                 "success": true   
             }';


        ?>
 ngOnInit() {


    this.auth.getUserDetails('admin','admin')
    .subscribe(
      data => {
        console.log('success',data);
        if(data.success){

        }
        else{
          window.alert("invalid");
        }

    },
      error=> {
        console.log('failed',error);


      }
  ) 
  var _this=this;
  setTimeout(function(){


    _this.user.getSomeData().subscribe(data=>{

      console.log(data);

    })

    }, 3000);


  }