Skip to content

Instantly share code, notes, and snippets.

@banago
Created March 28, 2012 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banago/2228063 to your computer and use it in GitHub Desktop.
Save banago/2228063 to your computer and use it in GitHub Desktop.
Tests if the current request is an AJAX request
<?php
/**
* Tests if the current request is an AJAX request by checking the X-Requested-With HTTP
* request header that most popular JS frameworks now set for AJAX calls.
*
* @return boolean
*/
function is_ajax()
{
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment