Last Updated: February 25, 2016
·
1.422K
· 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 ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside