How can I get PHP to display the headers it received from a browser?

How can I get PHP to display the headers it received from a browser?

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

Stefan Bogdanescu

Founder & Senior Architect

Accessing Incoming HTTP Headers in PHP

When a browser sends a request to your server, it bundles metadata into the HTTP headers. These headers tell you about the user agent, the content type of the request, or specific custom directives like X-Request-ID. While frameworks often abstract these details away, understanding the underlying mechanism is crucial for debugging, security logging, and handling complex API integrations.

In PHP, the primary repository for this data is the $_SERVER superglobal array. This associative array maps standard HTTP headers to their corresponding environment variables. However, relying solely on $_SERVER requires a nuanced understanding of how the server translates incoming header names into lowercase keys with underscores replacing hyphens.

The $_SERVER Array and Name Transformation

When you inspect $_SERVER, you will notice that header names are not stored exactly as they appear in the raw HTTP request. PHP normalizes these names by converting them to lowercase and replacing hyphens with underscores. For example, a custom header named X-Custom-Header becomes accessible via $_SERVER['X_CUSTOM_HEADER'].

It is important to note that $_SERVER does not contain all headers. Standard HTTP headers like Content-Type, Accept, and User-Agent are reliably mapped here. However, some headers might be missing depending on the SAPI (Server API) or specific server configurations. If a header is absent from $_SERVER, it is often because the web server (like Apache or Nginx) did not forward it to the PHP process, or the specific SAPI strips certain data for security reasons.

Handling Custom Headers and Edge Cases

You asked if they are all stored in $_SERVER, even custom ones. The short answer is "usually, but not always." Custom headers sent by a browser or an API client generally follow the same normalization rules as standard headers. If your application receives a header named X-API-Version: 1.2, you would check $_SERVER['X_API_VERSION'].

However, there are pitfalls. Some web servers might ignore headers that start with certain prefixes or contain invalid characters before passing them to the PHP application. Furthermore, older versions of PHP or specific SAPIs (like CLI) might not populate $_SERVER at all for incoming requests unless explicitly configured to do so. If you find a header missing in $_SERVER, it does not necessarily mean the browser didn't send it; it could be a server-side filtering issue.

Using getallheaders() for Raw Data

When $_SERVER feels insufficient—perhaps because you need the exact casing of the header or want to debug why a specific custom header is missing—you should use the getallheaders() function. This built-in function returns an associative array where keys retain their original case and hyphens are preserved.

$headers = getallheaders();

// Accessing a custom header while preserving its original format
if (isset($headers['X-Custom-Header'])) {
    echo "Custom Header Value: " . $headers['X-Custom-Header'];
} else {
    // Fallback to $_SERVER if the key varies due to server configuration
    if (isset($_SERVER['X_CUSTOM_HEADER'])) {
        echo "Custom Header Value: " . $_SERVER['X_CUSTOM_HEADER'];
    } else {
        echo "Header not found in standard locations.";
    }
}

The getallheaders() function is available in PHP 4.3 and later, making it a safe choice for legacy code as well as modern versions. It is particularly useful when dealing with strict API contracts where header case sensitivity matters, although technically HTTP headers are case-insensitive by specification, many clients and servers behave inconsistently in practice.

Trade-offs and Security Considerations

Before parsing these arrays blindly, consider the security implications. Header values can sometimes contain injection vectors if passed directly into output without escaping. Additionally, relying on $_SERVER assumes that the web server is configured to forward all necessary headers. In high-security environments, middleware might intentionally drop headers like X-Forwarded-For or custom tracking identifiers before they reach the application layer.

If you are building a robust application that needs to handle these details transparently regardless of the underlying PHP version or SAPI, encapsulating this logic in a service class is often better than scattering $_SERVER checks throughout your codebase. This approach isolates the complexity and makes it easier to mock during testing.

For developers looking for deeper insights into HTTP handling within the Laravel ecosystem, resources such as https://laravelcompany.com offer detailed documentation on managing request data and headers using the framework's expressive API, which wraps these low-level details in a more intuitive interface.

Our network