To detect an AJAX request in PHP there are several methods :
If you are using
Core PHP :
//Using Global Variables
1 2 3 4 5 6 | if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { echo "Its an AJAX request"; }else{ echo "Do something more"; } |
//With Apache request header method :
1 2 3 | $headers = apache_request_headers(); $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest'); echo $is_ajax; |
Codeigniter Framework:
Method :
1 | $this->input->is_ajax_request(); |
Response : Boolean