HOWTO · PHP

How to Get Headers in PHP

Learn how to retrieve response headers for a URL and request headers received by a PHP server.

On this page

HTTP headers carry metadata between an HTTP client and a server. Choose a PHP method based on which side of the exchange you need to inspect: get_headers() reads the response to a URL request, while $_SERVER and request-header functions expose information received by the current server.

Choose the PHP method based on which headers you need

Use get_headers() when your PHP code needs to inspect the response returned for a URL. To inspect the incoming request handled by your application, read a selected value from $_SERVER, or retrieve a collection with apache_request_headers() or getallheaders(). A filtered $_SERVER list is another option when those functions are unavailable or unsuitable.

These sources describe different parts of the exchange. A response header such as Content-Type describes the response; a request header such as User-Agent came from the client. The values available to an application can depend on the request and server configuration.

Get response headers for a URL with get_headers()

get_headers() makes a request to the given URL and returns the response headers as an array. The first array item commonly contains the HTTP status line, followed by header lines. The URL, redirects, server response, and network conditions affect the returned values, so do not rely on a fixed live-site dump.

<?php
$url = 'https://example.com/';
$headers = @get_headers($url);

if ($headers === false) {
    echo "No response headers were returned.\n";
} else {
    foreach ($headers as $header) {
        echo $header, "\n";
    }
}

This example requires a reachable URL and PHP’s URL stream support; its response is environment-dependent and is not shown as fixed output. In application code, validate or constrain URLs when they can come from users, and handle a failed request rather than assuming the result is always an array.

Read a single request header from $_SERVER

PHP commonly exposes incoming request headers as HTTP_* entries in $_SERVER. For example, a user-agent value may be available under HTTP_USER_AGENT. The array also includes other server and request information, and a particular header key may be absent. Use a fallback when reading optional values:

$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? null;

Treat the value as input supplied with the request. Validate it before using it in decisions or output, and escape it when rendering it into a page.

Get all request headers with apache_request_headers() or getallheaders()

apache_request_headers() returns the incoming request headers as an associative array when the function is available in the running environment. getallheaders() provides the same general purpose. Their availability depends on the PHP and server environment; check before calling either when code must run across different configurations.

<?php
if (function_exists('getallheaders')) {
    $headers = getallheaders();
} elseif (function_exists('apache_request_headers')) {
    $headers = apache_request_headers();
} else {
    $headers = [];
}

foreach ($headers as $name => $value) {
    echo $name, ': ', $value, "\n";
}

Run this in a web request handled by an environment that provides one of the functions. In a command-line process, there may be no incoming HTTP request or applicable server API, so the list can be empty.

Build a request-header list from $_SERVER

When you need a fallback list, filter $_SERVER for keys beginning with HTTP_, then remove that prefix and convert underscores to hyphens. The filter selects those entries only; it does not include every server variable. Some request data, such as content length or content type, may use separate $_SERVER keys rather than the HTTP_ prefix.

<?php
function requestHeadersFromServer(array $server): array
{
    $headers = [];
    foreach ($server as $key => $value) {
        if (strncmp($key, 'HTTP_', 5) !== 0) {
            continue;
        }
        $name = str_replace('_', '-', strtolower(substr($key, 5)));
        $headers[$name] = $value;
    }
    return $headers;
}

$headers = requestHeadersFromServer($_SERVER);
foreach ($headers as $name => $value) {
    echo $name, ': ', $value, "\n";
}

This conversion provides readable names such as user-agent; it does not reconstruct headers that the server omitted or stored under another key. Header names are case-insensitive in HTTP, so applications should not depend on a particular capitalization when comparing names. The displayed values vary with the incoming request and server environment.