if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { // This is an AJAX request // Process the request here } else { // This is not an AJAX request // Redirect the user or return an error message }
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { // This is an AJAX request // Get some data $data = array('username' => 'john', 'email' => 'john@example.com'); // Return JSON data header('Content-Type: application/json'); echo json_encode($data); exit; } else { // This is not an AJAX request // Redirect the user or return an error message }The isAjax function is part of the standard PHP library and does not require any additional package or library.