The Myth of $_SERVER['HTTP_X_REQUESTED_WITH'] in PHP
A persistent pattern across many online tutorials and Stack Overflow answers suggests a reliable method for detecting AJAX requests: checking if the header HTTP_X_REQUESTED_WITH equals XMLHttpRequest. The logic seems sound at first glance. If a browser makes an asynchronous call, it ostensibly sends this identifier. However, relying on this approach in modern PHP environments is problematic because the variable simply does not exist in standard configurations.
When you attempt to echo $_SERVER['HTTP_X_REQUESTED_WITH'], the output is often empty or null. This isn't necessarily a configuration error on your part; rather, it reflects how web servers and browsers behave in practice. The HTTP header X-Requested-With is indeed sent by many clients when making AJAX calls. However, PHP's superglobal $_SERVER populates headers only if the web server explicitly parses them into that array.
In Apache environments using mod_php, or standard setups without specific configurations, the raw incoming HTTP headers are not automatically populated into $_SERVER. Consequently, even if your browser sends the header, PHP cannot see it unless you have configured your server to parse all incoming headers. This is a common point of confusion for developers transitioning from JavaScript logic directly to backend validation.
The Server Configuration Gap
The absence of HTTP_X_REQUESTED_WITH in the $_SERVER array usually boils down to how the web server handles header parsing. In Apache, you can force the server to parse all incoming headers by adding the following directive to your .htaccess file or virtual host configuration:
SetEnvIf HTTP_X-Requested-With "XMLHttpRequest" AJAX_REQUEST=1
Even with this directive, the original header name might not appear directly in $_SERVER as a lowercase string depending on the server software. For example, Apache normalizes keys to uppercase, while Nginx behaves differently. If you are using Nginx with PHP-FPM, headers are typically passed via the FastCGI protocol, and they often arrive as HTTP_X_REQUESTED_WITH in $_SERVER, but this is not guaranteed across all hosting providers or server versions.
Because of this inconsistency, checking for this specific header is brittle. A robust application should not rely on a single flag that depends entirely on client behavior and server configuration quirks.
Modern Alternatives for Request Detection
If your goal is to distinguish between standard page loads and asynchronous requests, relying on $_SERVER['HTTP_X_REQUESTED_WITH'] is a bad strategy due to its fragility. Instead, consider inspecting the request method or specific headers that are more consistently available.
One alternative is checking the Accept header or using JavaScript's XMLHttpRequest object properties if you control both client and server logic tightly. However, a cleaner approach in many frameworks involves defining routes based on the nature of the endpoint itself rather than the transport mechanism.
In frameworks like Laravel, the request object provides more reliable methods to inspect headers without worrying about $_SERVER normalization issues. You can access the raw header using:
$request->header('X-Requested-With', 'unknown');
This approach works regardless of the underlying server configuration because it bypasses the $_SERVER array entirely, accessing the parsed headers directly from the framework's request object. This is a significant improvement over checking $_SERVER directly, which is often unreliable without specific Apache configurations.
Why We Should Stop Checking for AJAX Headers
Beyond technical reliability, there is an architectural argument against detecting AJAX requests based on headers. The distinction between "AJAX" and "standard" requests is increasingly arbitrary. Modern frontend frameworks like Vue, React, or Alpine.js handle routing and data fetching in ways that blur the line between traditional page loads and background data calls.
Furthermore, relying on X-Requested-With can lead to false negatives where legitimate AJAX calls are treated as standard requests, potentially breaking API endpoints or caching strategies. If you find yourself needing to differentiate request types, it is often better to enforce API contracts using content negotiation (checking the Accept header) or by designing your API endpoints to handle specific data formats regardless of how they were triggered.
Conclusion on Header Reliability
The short answer to whether $_SERVER['HTTP_X_REQUESTED_WITH'] exists in PHP is: it usually doesn't, unless your server is explicitly configured to parse all headers and you happen to be on a platform where that parsing happens automatically. For production code, relying on this variable is risky. It is safer to use framework-specific request objects or inspect the Accept header to determine how a client intends to consume your API's response. This ensures your application remains functional regardless of the specific web server software or configuration in use.