Identifying an Ajax Request with Php

Sharing is caring!

Here’s a fast bit of code that i find handy to check if a solicit that goes to a Php page was made through an Ajax call or a basic structure post. This system utilization the $_server[‘http_x_requested_with’] ask for to confirm if information was sent to a particular page utilizing a xmlhttprequest. It’s worth acknowledging that there is no certification that each web server will furnish this setting, servers might discard particular $_server parameters, That said, an imposing number of these variables are accounted.

if(isset($_server[‘http_x_requested_with’]) && !empty($_server[‘http_x_requested_with’]) && strtolower($_server[‘http_x_requested_with’]) == ‘xmlhttprequest’)

{

// If its an ajax solicitation execute the code beneath

reverberation ‘this is an ajax request!’;

retreat;

}

//provided that its not an ajax appeal resound the beneath.

reverberation ‘this is obviously not an ajax request!’;

The code is pretty obvious we are actually verifying whether the appeal was sent by means of a xmlhttprequest. In the Demo beneath i’ve setup a page that holds a jquery Ajax ask for to our code above and a modest shape with a bind that only submits to the same page.

2 thoughts on “Identifying an Ajax Request with Php

Leave a Reply

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

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange