How do I read any request header in PHP?

How do I read any request header in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How do I read any request header in PHP?

Reading custom HTTP headers like X-Requested-With is a common requirement when building APIs or handling JSON payloads. The standard approach relies on the $_SERVER superglobal array, which maps incoming HTTP headers to specific keys using a standardized prefix and case conversion. To access a header named X-Custom-Header, you would typically check $_SERVER['HTTP_X_CUSTOM_HEADER']. Note that this key only exists if the header is present in the request; accessing it without checking first will result in an undefined array index warning or null value depending on your error reporting settings.

For frameworks like Laravel, the syntax shifts to a more readable and consistent interface using the Request class. You can retrieve any header via the header() helper method or the headers property. For instance, $request->header('X-Requested-With') returns the raw value of that specific header, defaulting to null if it's missing. This abstraction handles the underlying HTTP protocol nuances, such as stripping whitespace and normalizing casing, without exposing you to the quirks of the native $_SERVER array.

The Nature of Header Keys

Understanding how headers are translated into server variables is critical for writing robust checks. PHP follows RFC 2616 conventions when populating $_SERVER. Every header name is prefixed with HTTP_, and internal delimiters are replaced with underscores. Furthermore, the entire header name is converted to uppercase before this transformation occurs.

If you need to read a header that does not start with "X-", such as Content-Type or Authorization, the prefix rule still applies. A request containing Content-Type: application/json will appear in $_SERVER as HTTP_CONTENT_TYPE. However, if the header name contains special characters like colons or spaces, they are removed and replaced with underscores. This normalization ensures consistency but means you cannot rely on case-sensitive matches against the raw header string sent by the client.

Handling Null Values Safely

A frequent pitfall when accessing headers is attempting to use them in logical conditions without verifying their existence first. In PHP, comparing a null value to an empty string using the loose equality operator (==) evaluates to true. This can lead to unexpected behavior where a missing header is treated as if it contained an empty string.

Consider the following scenario when checking for a custom header:

if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
    // Logic here
}

If the header is absent, $_SERVER['HTTP_X_REQUESTED_WITH'] is null. The comparison null == '' returns true, causing your conditional block to execute even though no header was provided. To avoid this, you must explicitly check for null or use the strict equality operator (===). Alternatively, checking if the key exists using array_key_exists() ensures that you only proceed when the header is genuinely present in the request.

Framework Abstractions and Security

When working within a framework environment, relying on the framework's request object is generally preferred over direct access to $_SERVER. Frameworks like Laravel provide methods to safely access headers that handle edge cases internally. For example, $request->header('X-Requested-With', 'default') allows you to set a default value if the header is missing, ensuring your application never crashes due to undefined indices.

While checking headers is straightforward, be cautious about trusting their content for security decisions. Headers are easily spoofed by clients. If you rely on headers like X-Requested-With to determine how to process data, ensure that the logic doesn't inadvertently expose sensitive functionality or bypass authentication layers based solely on a header value. Always validate input regardless of the source.

For more complex routing needs involving specific headers, tools like Laravel offer middleware patterns to intercept and inspect requests globally before they reach your controllers. This keeps your business logic clean and centralizes security policies related to incoming data.

Our network