Last Updated: February 25, 2016
·
1.215K
· daniel-melzer

Check if a request is an AJAX request in one line

To find out if a request was performed via AJAX, you can use this oneliner:

isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']

1 Response
Add your response

This protip only works when passed the parameter and isn't a fully secure way of checking it's an XMLHttpRequest.

This is also quite an old method of detection (https://github.com/cybershade/CSCMS/blob/master/core/constants.php#L28).

Also, your code should be following grammatical standards, so your code should be

isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']
over 1 year ago ·