title: How to use the same PHP session in every fetch?
body: <p>I am trying to make a simple authentication in Javascript using fetch to post credentials to a PHP file (login.php) inside this file it simply authenticates and add some values to $_SESSION and after receiving a response I immediately invoke another get fetch to (sessioninfo.php) and this php file simply returns the $_SESSION for testing purposes, but I received an empty array even though I already invoked session_start() in (sessioninfo.php) and (login.php).</p>
  <p>I tried using axios and console.log the response and every response show different PHP Session ID, which I think the cause of why the $_SESSION is empty.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>fetch("http://localhost/login.php/", {
      method: 'POST',
      body: JSON.stringify({username: 'test', password: 'test'})    
  })
  .then(res =&gt; res.json())
  .then(data =&gt; {
      fetch("http://localhost/sessioninfo.php")
      .then(res =&gt; res.json())
      .then(data =&gt; console.log(data));
  });</code></pre>
  </div>
  </div>
  </p>
  <p>Edited:</p>
  <p>login.php</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;?php
  session_start();
  header("Content-Type: application/json");
  require_once "../../pdo.php";
  
  /**
   * @var PDO $pdo
   */
  
  $data = json_decode(file_get_contents('php://input'), true);
  $sql = "SELECT user_id from users WHERE username=:uname AND password=:pwd";
  header("Content-Type: application/json");
  $isInit = isset($data['username']) &amp;&amp; isset($data['password']);
  if($isInit) {
      $stmt = $pdo-&gt;prepare(
          $sql);
      $stmt-&gt;execute(array(
          ':uname' =&gt; $data['username'],
          ':pwd' =&gt; $data['password']
      ));
      $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
      
      if($row != false) {
          $sessionHash = hash('md5', $data['username'].$data['password'].$row['user_id']);
          $_SESSION['ca'] = $sessionHash;
          $_SESSION['user_id'] = $row['user_id'];
          $response = array('isAuth' =&gt; true, 'user_id' =&gt; $row['user_id'], 'ca' =&gt; $sessionHash, 'session' =&gt; $_SESSION);
          echo json_encode($response);
      }
      else {
          $response = array('isAuth' =&gt; false);
          echo json_encode($response);
      }
  }
  else {
      $response = array('message' =&gt; 'Missing input');
      echo json_encode($response);
  }</code></pre>
  </div>
  </div>
  </p>
  <p>I think there is no problem in login.php because I tested it with the right username and password and returns the right response</p>
  <p>sessioninfo.php</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;?php
  session_start();
  
  echo json_encode($_SESSION);</code></pre>
  </div>
  </div>
  </p>