Skip to content
Oct 27 / Scott Phillips

Get the Headers of Incoming Requests Using PHP

It’s pretty easy to get the headers of incoming requests using PHP. If you’re using Apache, you can simple do something like:

$headers = apache_request_headers();

foreach ($headers as $header => $value) {
    echo "$header: $value <br />\n";
}

If you’re not using Apache, your best bet is to try and extract them from the $_SERVER variable. Something like this would do the trick:

print_r(getHeaders());

function getHeaders()
{
    $headers = array();
    foreach ($_SERVER as $k => $v)
    {
        if (substr($k, 0, 5) == "HTTP_")
        {
            $k = str_replace('_', ' ', substr($k, 5));
            $k = str_replace(' ', '-', ucwords(strtolower($k)));
            $headers[$k] = $v;
        }
    }
    return $headers;
}

 

Leave a comment