PHP 5.4.36 Released

apache_request_headers

(PHP 4 >= 4.3.0, PHP 5)

apache_request_headersLiefert alle HTTP-Requestheader

Beschreibung

array apache_request_headers ( void )

Liefert alle HTTP-Requestheader von der aktuellen Anfrage.

Rückgabewerte

Ein assoziatives Array aller HTTP-Header der aktuellen Anfrage, im Fehlerfall wird FALSE zurückgegeben.

Changelog

Version Beschreibung
5.4.0 Diese Funktion wurde unter FastCGI verfügbar. Vorher wurde sie nur unterstützt, wenn PHP als Apache Modul installiert war.
4.3.3

Ab PHP 4.3.3 können Sie diese Funktion auch mit dem NSAPI-Server-Modul für Netscape/iPlanet/SunONE-Webserver nutzen.

Beispiele

Beispiel #1 apache_request_headers()-Beispiel

<?php
$headers 
apache_request_headers();

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

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

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

Anmerkungen

Hinweis:

Die Werte der gebräuchlichsten CGI-Variablen können Sie auch durch das Auslesen der Umgebungsvariablen erhalten. Dieser Weg funktioniert unabhängig davon, ob Sie PHP als Modul für Apache benutzen. Verwenden Sie phpinfo(), um eine Liste aller zur Verfügung stehenden Umgebungsvariablen zu erhalten.

Siehe auch

add a note add a note

User Contributed Notes 3 notes

up
5
limalopex.eisfux.de
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:

<?php
if( !function_exists('apache_request_headers') ) {
///
function apache_request_headers() {
 
$arh = array();
 
$rx_http = '/\AHTTP_/';
  foreach(
$_SERVER as $key => $val) {
    if(
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);
      if(
count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
        foreach(
$rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
       
$arh_key = implode('-', $rx_matches);
      }
     
$arh[$arh_key] = $val;
    }
  }
  return(
$arh );
}
///
}
///
?>
up
4
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
<?php
  $_SERVER
['HTTP_IF_MODIFIED_SINCE'];
 
$_SERVER['HTTP_IF_NONE_MATCH'];
?>

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.
up
0
uli dot staerk at globalways dot net
3 days ago
A slightly modified version from  limalopex.eisfux.de. Fixes the missing Headers Content-Type and Content-Length and makes it Camel-Case.

<?php
if( !function_exists('apache_request_headers') ) {
        function
apache_request_headers() {
               
$arh = array();
               
$rx_http = '/\AHTTP_/';
                foreach(
$_SERVER as $key => $val) {
                        if(
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('_', strtolower($arh_key));
                                if(
count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
                                        foreach(
$rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
                                       
$arh_key = implode('-', $rx_matches);
                                }
                               
$arh[$arh_key] = $val;
                        }
                }
                if(isset(
$_SERVER['CONTENT_TYPE'])) $arh['Content-Type'] = $_SERVER['CONTENT_TYPE'];
                if(isset(
$_SERVER['CONTENT_LENGTH'])) $arh['Content-Length'] = $_SERVER['CONTENT_LENGTH'];
                return(
$arh );
        }
}
To Top