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']
Written by Daniel Melzer
Related protips
1 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
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#