apache_request_headers

(PHP 4 >= 4.3.0, PHP 5)

apache_request_headers -- Fetch all HTTP request headers

说明

array apache_request_headers ( void )

Fetches all HTTP requests from the current request.

此函数仅在 PHP 作为 Apache 模块安装时才可使用。

返回值

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

范例

例子 1. apache_request_headers() example

<?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

注释

注: Prior to PHP 4.3.0, apache_request_headers() was called getallheaders(). After PHP 4.3.0, getallheaders() is an alias for apache_request_headers().

注: You can also get at the value of the common CGI variables by reading them from the environment, which works whether or not you are using PHP as an Apache module. Use phpinfo() to see a list of all of the available environment variables.

注: 自 PHP 4.3.3 起也可以在 Netscape/iPlanet/SunONE 服务器的 NSAPI server module 中使用本函数。


add a note add a note User Contributed Notes
limalopex.eisfux.de
30-Oct-2006 01:01
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 );
}
///
}
///
?>
php [at] barryhunter [.] co [.] uk
09-Sep-2005 07:00
Surly this could be rewritten as

function list_dirs($path, $target)
{
   $list = scandir($path);
   $url = $_SERVER['HTTP_HOST'];
    
   foreach ($list as $number => $filename)
   {
       if ( $filename !== '.' && $filename !== '..' && !is_file($filename) )
       {
           if ($target == '')
           {
               // Print Dirs with link
               print ("<a href=\"http://$url/$filename\">$filename</a> <br>\n");
           }
           else
           {
               // Print Dirs with link
               print ("<a href=\"http://$url/$filename\" target=\"$target\">$filename</a> <br>\n");
           }
              
       }
   }
}

which would then not require apache. I havent tested this but looks to do exactly the same.
renich at woralelandia dot com
22-Apr-2005 01:50
Here is a simple listing function. It accepts a path and target. Examples listed bellow

function list_dirs($path, $target)
{
   $list = scandir($path);
  
   foreach ($list as $number => $filename)
   {
       if ( $filename !== '.' && $filename !== '..' && !is_file($filename) )
       {
           // Asign more readable and logic variables
           $dir = $filename;
           $url = apache_request_headers();
          
           if ($target == '')
           {
               // Print Dirs with link
               print ("<a href=\"http://$url[Host]/$dir\">$dir</a> <br>\n");
           }
           else
           {
               // Print Dirs with link
               print ("<a href=\"http://$url[Host]/$dir\" target=\"$target\">$dir</a> <br>\n");
           }
              
       }
   }
}

Examples:

1.- List actual dir with no target option

<?php

list_dirs
('.', '');

?>

2.- List "mydir" with a "_blank" as target name

<?php

list_dirs
('/home/renich/www/mydir', '_blank')

?>

Notes:
- Its a simple function and it uses a function that interacts with apache. I don't know what will happen if you request a file list of a directory outside of the apache realm!
- Not fully tested!