apache_request_headers

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

apache_request_headers모든 HTTP 요청 헤더를 가져옵니다

설명

array apache_request_headers ( void )

현재 요청에서 모든 HTTP 요청 헤더를 가져옵니다.

이 함수는 PHP를 아파치 모듈로 설치하였을 때만 지원합니다.

반환값

현재 요청의 모든 HTTP 헤더에 대한 연관 배열이거나, 실패시엔 FALSE.

예제

Example #1 apache_request_headers() 예제

<?php
$headers 
apache_request_headers();

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

위 예제의 출력 예시:

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

주의

Note:

PHP 4.3.0 이전에, apache_request_headers()getallheaders()로 불리워졌습니다. PHP 4.3.0 이후의 getallheaders()apache_request_headers()의 단축형입니다.

Note:

환경 변수에서 일반 CGI 변수를 읽어서, PHP를 Apache 모듈로 사용하고 있는지를 확인할 수 있습니다. 모든 사용 가능한 환경 변수 목록은 phpinfo()를 이용해서 볼 수 있습니다.

PHP 4.3.3부터 이 함수를 넷스케이프/iPlanet/SunONE 웹서버의 NSAPI 서버 모듈에서도 사용할 수 있습니다.

참고

add a note add a note

User Contributed Notes 5 notes

up
15
limalopex.eisfux.de
17 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
6
Tel
4 years ago
Although we expect to see headers in mixed case, the standard RFC2616 demands that "field names are case-insensitive". PHP delivers the headers exactly untouched in whatever way the client sent them. Potentially you should expect to get any type of uppercase or lowercase or mixed.

Thus, if you want to be standards compliant, you must loop through every key and check it in a case-insensitive manner, instead of doing the obvious thing and using the name of the header as an array index.
up
9
callum85 at notspam dot msn dot com
17 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
6
egocentryk
9 years ago
Superglobal $_SERVER,  used in all patches for missing getallheaders() contains only truly basic headers.  To pass ANY header into PHP in any httpd environment, including CGI/FCGI just add rule (any number of rules) into .htaccess:

RewriteRule .* - [E=HTTP_MY_HEADER:%{HTTP:My-Header}]

and the header with it's value will appear for PHP as
<?php print $_SERVER['HTTP_MY_HEADER']; ?>

And... just couldn't hold off. Rewrtiting $_SERVER keys for replacing missing function really does not require RegExps, preg_matches or evals... Try this:
<?php
function getallheaders() {
  foreach(
$_SERVER as $K=>$V){$a=explode('_' ,$K);
      if(
array_shift($a)=='HTTP'){
          
array_walk($a,function(&$v){$v=ucfirst(strtolower($v));});
      
$retval[join('-',$a)]=$V;}
    } return
$retval; }
?>
up
-9
uli dot staerk at globalways dot net
9 years 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