(PHP 4 >= 4.3.0, PHP 5)

apache_request_headersObtem todos os headers HTTP


array apache_request_headers ( void )

Obtém todos os cabeçalhos HTTP da requisição atual.

This function is only supported when PHP is installed as an Apache module.

Valor Retornado

An associative array of all the HTTP headers in the current request, or FALSE on failure.


Exemplo #1 Exemplo apache_request_headers()


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

O exemplo acima irá imprimir algo similar à:

Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: www.example.com
Connection: Keep-Alive



Antes do PHP 4.3.0, apache_request_headers() era chamado getallheaders(). Depois do PHP 4.3.0, getallheaders() é um sinônimo para apache_request_headers().


Você também pode obter o valor das variáveis CGI comuns lendo-as a partir do ambiente, o que funciona caso esteja ou não utilizando o PHP como módulo do Apache. Faça um phpinfo() para ver uma lista dessas variáveis chamadas variáveis ambiente.

As of PHP 4.3.3 you can use this function with the NSAPI server module in Netscape/iPlanet/SunONE webservers, too.

Veja Também

add a note add a note

User Contributed Notes 2 notes

callum85 at notspam dot msn dot com
7 years ago
There is a simple way to get request headers from Apache even on PHP running as a CGI. As far as I know, it's the only way to get the headers "If-Modified-Since" and "If-None-Match" when apache_request_headers() isn't available. You need mod_rewrite, which most web hosts seem to have enabled. Put this in an .htacess file in your web root:

RewriteEngine on
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

The headers are then available in PHP as

I've tested this on PHP/5.1.6, on both Apache/2.2.3/Win32 and Apache/2.0.54/Unix, and it works perfectly.

Note: if you use RewriteRules already for clean URLs, you need to put the above rules AFTER your existing ones.
8 years ago
I didn't found a replacement for apache_request_headers() in PHP::Compat (http://pear.php.net/package/PHP_Compat) so I wrote my own:

if( !function_exists('apache_request_headers') ) {
function apache_request_headers() {
$arh = array();
$rx_http = '/\AHTTP_/';
$_SERVER as $key => $val) {
preg_match($rx_http, $key) ) {
$arh_key = preg_replace($rx_http, '', $key);
$rx_matches = array();
// do some nasty string manipulations to restore the original letter case
      // this should work in most cases
$rx_matches = explode('_', $arh_key);
count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
$rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
$arh_key = implode('-', $rx_matches);
$arh[$arh_key] = $val;
$arh );
To Top