How to Detect an AJAX Request in PHP

In this tutorial, we are going to see how to detect an ajax request in PHP. Whenever we use ajax in our web projects, we must check every request is an ajax request before giving response.

So, this tutorial will help you to understand how to detect an ajax request in PHP.

Let’s see the snippet.

PHP

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&  strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
{
echo "AJAX request";
} else{
echo "No";
}

Leave a Comment