downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | sites | conferences | my

search for in the

apache_reset_timeoutspacer spacer apache_note
[edit] Last updated: Fri, 07 Sep 2012

view this page in


(PHP 4 >= 4.3.0, PHP 5)

apache_request_headersFetch all HTTP request headers


array apache_request_headers ( void )

Fetches all HTTP request headers from the current request.

Return Values

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


Version Description
5.4.0 This function became available under FastCGI. Previously, it was supported only when PHP was installed as an Apache module.

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


Example #1 apache_request_headers() example


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

The above example will output something similar to:

Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Connection: Keep-Alive



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.

See Also

  • apache_response_headers() - Fetch all HTTP response headers

apache_reset_timeoutspacer spacer apache_note
[edit] Last updated: Fri, 07 Sep 2012
spacer add a note User Contributed Notes apache_request_headers
callum85 at notspam dot msn dot com 18-Apr-2007 11: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

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.
jrabbit 21-Jan-2007 05: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')) {
        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)))));
            return $out;
?> 29-Oct-2006 09:01
I didn't found a replacement for apache_request_headers() in 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 );
spacer add a note

show source | credits | sitemap | contact | advertising | mirror sites is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.