How to detect AJAX request in PHP


Here are some quick PHP snippets that you can use to detect AJAX request in PHP:

Method 1:

We use the function apache_request_headers to detect AJAX request in PHP

function is_ajax()
{
    $request_headers = apache_request_headers();
    return (isset($request_headers['X-Requested-With']) && $request_headers['X-Requested-With'] == 'XMLHttpRequest');
}

Note: $request_headers will have the value like:

array(9) {
  ["Host"]=>
  string(11) "tutorialspots.com"
  ["Connection"]=>
  string(10) "keep-alive"
  ["Accept"]=>
  string(3) "*/*"
  ["X-Requested-With"]=>
  string(14) "XMLHttpRequest"
  ["User-Agent"]=>
  string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
  ["Referer"]=>
  string(30) "http://tutorialspots.com/"
  ["Accept-Encoding"]=>
  string(19) "gzip, deflate, sdch"
  ["Accept-Language"]=>
  string(33) "en-US,en;q=0.8,vi;q=0.6,und;q=0.4"
  ["Cookie"]=>
  string(191) "G_AUTHUSER_H=0; G_ENABLED_IDPS=google; PHPSESSID=1984374d004a5bac6c91097c5c08716c; __cfduid=d8039e1101ba9d259e3e6e65a561601121472152529; _gat=1; _ga=GA1.2.316412006.1467819185; G_AUTHUSER_H=0"
}

Method 2:

Use the variable $_SERVER

function is_ajax2()
{
    $header = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : null;
    return ($header === 'XMLHttpRequest');
}

Note: $_SERVER will have the value like:

array(38) {
  ["REDIRECT_STATUS"]=>
  string(3) "200"
  ["HTTP_HOST"]=>
  string(11) "tutorialspots.com"
  ["HTTP_CONNECTION"]=>
  string(10) "keep-alive"
  ["HTTP_ACCEPT"]=>
  string(3) "*/*"
  ["HTTP_X_REQUESTED_WITH"]=>
  string(14) "XMLHttpRequest"
  ["HTTP_USER_AGENT"]=>
  string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
  ["HTTP_REFERER"]=>
  string(30) "http://tutorialspots.com/"
  ["HTTP_ACCEPT_ENCODING"]=>
  string(19) "gzip, deflate, sdch"
  ["HTTP_ACCEPT_LANGUAGE"]=>
  string(33) "en-US,en;q=0.8,vi;q=0.6,und;q=0.4"
  ["HTTP_COOKIE"]=>
  string(191) "G_AUTHUSER_H=0; G_ENABLED_IDPS=google; PHPSESSID=1984374d004a5bac6c91097c5c08716c; __cfduid=d8039e1101ba9d259e3e6e65a561601121472152529; _gat=1; _ga=GA1.2.316412006.1467819185; G_AUTHUSER_H=0"
  ["PATH"]=>
  string(699) "C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\Microsoft Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_66\bin;C:\Program Files (x86)\QuickTime\QTSystem\;D:\HaxeToolkit\haxe\;D:\HaxeToolkit\neko;C:\ProgramData\ComposerSetup\bin;C:\Program Files (x86)\Skype\Phone\;"
  ["SystemRoot"]=>
  string(10) "C:\Windows"
  ["COMSPEC"]=>
  string(27) "C:\Windows\system32\cmd.exe"
  ["PATHEXT"]=>
  string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
  ["WINDIR"]=>
  string(10) "C:\Windows"
  ["SERVER_SIGNATURE"]=>
  string(80) "<address>Apache/2.2.8 (Win32) PHP/5.4.0 Server at loadfile.co Port 80</address>
"
  ["SERVER_SOFTWARE"]=>
  string(30) "Apache/2.2.8 (Win32) PHP/5.4.0"
  ["SERVER_NAME"]=>
  string(11) "loadfile.co"
  ["SERVER_ADDR"]=>
  string(9) "127.0.0.1"
  ["SERVER_PORT"]=>
  string(2) "80"
  ["REMOTE_ADDR"]=>
  string(9) "127.0.0.1"
  ["DOCUMENT_ROOT"]=>
  string(23) "D:/AppServ/www/tutorialspots"
  ["SERVER_ADMIN"]=>
  string(21) "webmaster@tutorialspots.com"
  ["SCRIPT_FILENAME"]=>
  string(33) "D:/AppServ/www/tutorialspots/index.php"
  ["REMOTE_PORT"]=>
  string(5) "64385"
  ["REDIRECT_QUERY_STRING"]=>
  string(46) "__hth=manager/ads"
  ["REDIRECT_URL"]=>
  string(41) "/manager/ads"
  ["GATEWAY_INTERFACE"]=>
  string(7) "CGI/1.1"
  ["SERVER_PROTOCOL"]=>
  string(8) "HTTP/1.1"
  ["REQUEST_METHOD"]=>
  string(3) "GET"
  ["QUERY_STRING"]=>
  string(46) "__hth=manager/ads"
  ["REQUEST_URI"]=>
  string(41) "/manager/ads"
  ["SCRIPT_NAME"]=>
  string(10) "/index.php"
  ["PHP_SELF"]=>
  string(10) "/index.php"
  ["REQUEST_TIME_FLOAT"]=>
  float(1474556431.43)
  ["REQUEST_TIME"]=>
  int(1474556431)
  ["argv"]=>
  array(1) {
    [0]=>
    string(46) "__hth=manager/ads"
  }
  ["argc"]=>
  int(1)
}

Leave a Reply