Check whether request is made from Ajax – PHP


Read {count} times since 2020

To check whether a request has been made is from Ajax or not you can use $_SERVER[‘HTTP_X_REQUESTED_WITH’]. See the following example:

if(strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’])==’xmlhttprequest’){
 echo "The request is from an Ajax Call";
}else{
 echo "The request is not from an Ajax Call";
}
?>

There you go. You now know how to check whether request made to file is from an Ajax Call or not in PHP.