Detect a fetch request in PHP

Viewed 4410

How can I detect requests from the Fetch API in PHP? I am currently using the approach below to detect an AJAX request:

$context['isAJAX'] = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

Ideally I could detect Fetch requests in a similar manner. Does anyone have any techniques to accomplish this?

3 Answers

Add to fetch options headers: {'X-Requested-With': 'XMLHttpRequest'}

Add this:

  fetch(url, {
    headers: {
      'credentials': 'same-origin',
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/json'
       // or 'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: 'POST'
  })
Related