How to check if the request is an AJAX request with PHP -
i check server-side if request php page ajax request or not.
i saw 2 ways this:
first way: sending get
parameter in request tells page ajax request (=mypage.php?ajax
)
mypage.php:
if(isset($_get['ajax'])) { //this ajax request, process data here. }
second way: set header xmlhttprequest
:
client-side js:
xmlhttprequestobject.open(“get”,url,true); xmlhttprequestobject.setrequestheader('x-requested-with', 'xmlhttprequest');
mypage.php:
if (!empty($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest' ) { //request ajax }
the fact is, 2 ways of doing can hacked, it's not secure check if ajax request this.
how can check if i'm receiving ajax request?
there no sure-fire way of knowing request made via ajax. can never trust data coming client. use couple of different methods can overcome spoofing.
Comments
Post a Comment