PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

apache_reset_timeout> <apache_note
Last updated: Fri, 26 Sep 2008

view this page in

apache_request_headers

(PHP 4 >= 4.3.0, PHP 5)

apache_request_headersRécupère tous les en-têtes HTTP de la requête

Description

array apache_request_headers ( void )

Récupère tous les en-têtes HTTP de la requête.

Cette fonction n'est supportée que si PHP est installé en tant que module d'Apache.

Valeurs de retour

Un tableau associatif avec tous les en-têtes HTTP de la requête courante ou FALSE en cas d'échec.

Exemples

Exemple #1 Exemple avec apache_request_headers()

<?php
$headers 
apache_request_headers();

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

L'exemple ci-dessus va afficher quelque chose de similaire à :

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

Notes

Note: Avant la version PHP 4.3.0, apache_request_headers() était appelée getallheaders(). Depuis PHP 4.3.0, getallheaders() est un alias de apache_request_headers().

Note: Vous pouvez aussi obtenir les valeurs des variables CGI communes en les lisant dans l'environnement, ce qui fonctionne, que vous soyez ou non en module Apache. Utilisez la fonction phpinfo() pour connaître la liste des variables d'environnement disponibles.

Note: Depuis PHP 4.3.3, vous pouvez utiliser cette fonction avec le module NSAPI des serveurs Netscape/iPlanet/SunONE.



apache_reset_timeout> <apache_note
Last updated: Fri, 26 Sep 2008
 
add a note add a note User Contributed Notes
apache_request_headers
tehjosh at gamingg dot net
30-Jul-2007 04:16
To callum85 at notspam dot msn dot com:

All of the request headers, including If-None-Match and If-Modified-Since should be available in the $_SERVER array by default.  Try running this code:

<?php
var_dump
($_SERVER);
?>

Experiment by sending different headers in the request to that script and you should see that the headers populate the $_SERVER array automatically.
callum85 at notspam dot msn dot com
19-Apr-2007 08:07
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.
tehjosh at gamingg dot net
18-Mar-2007 05:03
I wrote a function similar to jrabbit's, except that mine creates a custom function rather than redefining the original.
<?php
function request_headers()
{
    if(
function_exists("apache_request_headers")) // If apache_request_headers() exists...
   
{
        if(
$headers = apache_request_headers()) // And works...
       
{
            return
$headers; // Use it
       
}
    }

   
$headers = array();

    foreach(
array_keys($_SERVER) as $skey)
    {
        if(
substr($skey, 0, 5) == "HTTP_")
        {
           
$headername = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($skey, 0, 5)))));
           
$headers[$headername] = $_SERVER[$skey];
        }
    }

    return
$headers;
}
?>

Also, eval() is not necessary to define a function that is within an if block, as is done in jrabbit's code, because PHP will only read the function if it passes the if condition.
suso at suso dot org
18-Feb-2007 05:01
The PHP developers should not be creating functions like this that only work in the module version and not in the CGI version.  It creates situations where people who have setup special installations of PHP (like suphp) run into issues with prewritten software.  If you can't make it work with both, then don't make it work at all.
jrabbit
21-Jan-2007 02:04
The following code will implement an approximation of apache_request_headers for lighttpd making most scripts that use the function portable between the two platforms.

Caveats are:

1. This function will convert any _ in a header key to a -

2. This function will capitalize the first character and first character after each hyphen in each header key and lower case the rest of the key.

This does not cause a problem with standard headers which are capitalized in this manner but may cause custom headers to appear in a different case to apache.

if (!function_exists('apache_request_headers')) {
    eval('
        function apache_request_headers() {
            foreach($_SERVER as $key=>$value) {
                if (substr($key,0,5)=="HTTP_") {
                    $key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
                    $out[$key]=$value;
                }
            }
            return $out;
        }
    ');
}
limalopex.eisfux.de
29-Oct-2006 06: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 01: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
21-Apr-2005 07: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!

apache_reset_timeout> <apache_note
Last updated: Fri, 26 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites