How to detect an AJAX request in PHP

You can use below code block in your PHP scripts to check if an incoming HTTP request is made with AJAX or not. This can be useful in situations where you want to make sure your script only respond to asynchronous requests.

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
	// this is an ajax request
}

It is important to note that HTTP_X_REQUESTED_WITH header is not enabled in all servers. So having other checks in place is a good practice.

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar



Do NOT follow this link or you will be banned from the site!