Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch Api unable to get Session from PHP server

I am using Fetch Api in my application.

I've got a PHP server page to get session data which was already defined before. It seemd like this:

<?php
header('Content-Type: application/json; charset=UTF-8');
header('Access-Control-Allow-Origin: *');

session_start();
// $_SESSION['data'] already defined before
$result = array();
// print_r($_SESSION['data']);
if (isset($_SESSION['data'])) {
  $result = $_SESSION['data'];
  $result['code'] = 'ok';
} else {
  $result['code'] = 'error';
}
echo json_encode($result, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

I also got another html page to get the session data. It seemd like this:

  <script>
    $(function() {
      // use $.ajax
      $.ajax({
          url: 'session.php',
          dataType: 'json'
        })
        .done(function(res) {
          console.log(res);
        });
      // end

      // use fetch
      fetch('session.php').then(function(res) {
        if (res.ok) {
          res.json().then(function(obj) {
            console.log(obj);
          });
        }
      });
      // end
    });
  </script>

The problem is, when I use $.ajax(), session data can be correctly showed. But when I use fetch(), the session data was undefined.

So, what goes wrong and how can I fix it? Thanks!

like image 865
ogoss Avatar asked Apr 20 '16 10:04

ogoss


People also ask

Can we use session in REST API PHP?

REST API's are meant to be stateless. What that means is that each request from a client should include all the information needed to process the request. In other words, if you are writing a REST API in PHP then you should not be using $_SESSION to store data about the client's session.

How do you activate session in PHP?

Start a PHP Session A session is started with the session_start() function. Session variables are set with the PHP global variable: $_SESSION.

Why session_start () is used in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

What does $_ session do in PHP?

Starting a PHP Session Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.


2 Answers

If you want fetch to send cookies, you have to provide the credentials option.

See https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch#Parameters for details.

like image 188
Marco Castelluccio Avatar answered Sep 19 '22 15:09

Marco Castelluccio


jquery ajax is a usual ajax request and the browser is sending the cookie header with the session id that identify your session.

fetch doesnt - instead a new session is created with out any data send the php session id either with url or header

have a look at: http://php.net/manual/en/session.idpassing.php

like image 37
di3 Avatar answered Sep 18 '22 15:09

di3