How to check if the request is made via ajax in PHP

By | July 19, 2015

How to check if the request is made via ajax in PHP

[dropcap]N[/dropcap]owadays most of the website uses ajax requests for different purposes. To load a page, to load extra contents, to load images, and many others. And many of the sites are using the ajax request in the login and registration forms. So how to find if the request to the login page is made using ajax or direct request.

Ajax – Asynchronous JavaScript and XML.

AJAX is the art of exchanging data with a server, and updating parts of a web page – without reloading the whole page.

To find the request type we can use SERVER variables. Please use the below code to check if the request is made via ajax in php.

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
/* special ajax here */
// do more with ajax request
}

🔥0

One thought on “How to check if the request is made via ajax in PHP

Leave a Reply