Skip to content

Instantly share code, notes, and snippets.

@piercemoore
Created March 4, 2012 00:46
Show Gist options
  • Select an option

  • Save piercemoore/1969492 to your computer and use it in GitHub Desktop.

Select an option

Save piercemoore/1969492 to your computer and use it in GitHub Desktop.
Super Simple AJAX Detection in PHP
<?php
/*
* isAjax determines whether a server request is a standard browser-based request or if it is being loaded via AJAX.
*
* REMEMBER: Do NOT use this for security purposes as it is VERY easy to spoof headers.
*
* @return bool
*/
function isAjax() {
$param = $_SERVER['HTTP_X_REQUESTED_WITH'];
if( ( isset( $param ) && !empty( $param ) ) && ( ( strcmp( $param , 'XMLHttpRequest') ) == 0 || ( strcmp( $param , 'xmlhttprequest' ) == 0 ) ) ) {
// Yep. It's AJAX.
return true;
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment