雖然我們預期會看到大小寫混合的標頭,但標準 RFC2616 要求「欄位名稱不區分大小寫」。PHP 會原封不動地傳遞客戶端傳送的標頭,不論其大小寫為何。您可能會收到任何類型的大小寫或混合大小寫的標頭。
因此,如果您想要符合標準,則必須迴圈檢查每個鍵值,並以不區分大小寫的方式進行檢查,而不是使用標頭名稱作為陣列索引。
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
apache_request_headers — 擷取所有 HTTP 請求標頭
此函式沒有參數。
包含目前請求中所有 HTTP 標頭的關聯式陣列。
版本 | 說明 |
---|---|
7.3.0 | 此函式在 FPM SAPI 中可用。 |
範例 #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
雖然我們預期會看到大小寫混合的標頭,但標準 RFC2616 要求「欄位名稱不區分大小寫」。PHP 會原封不動地傳遞客戶端傳送的標頭,不論其大小寫為何。您可能會收到任何類型的大小寫或混合大小寫的標頭。
因此,如果您想要符合標準,則必須迴圈檢查每個鍵值,並以不區分大小寫的方式進行檢查,而不是使用標頭名稱作為陣列索引。
我在 PHP::Compat (http://pear.php.net/package/PHP_Compat) 中找不到 apache_request_headers() 的替代方案,所以我寫了自己的版本。
<?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();
// 進行一些字串操作以還原原始字母大小寫
// 這在大部分情況下應該都能正常運作
$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 );
}
///
}
///
?>
即使在以 CGI 模式執行的 PHP 中,也有一種簡單的方法可以從 Apache 取得請求標頭。據我所知,這是唯一一種在 apache_request_headers() 無法使用時取得「If-Modified-Since」和「If-None-Match」標頭的方法。您需要 mod_rewrite 模組,大多數虛擬主機似乎都已啟用它。將以下程式碼放入網站根目錄的 .htacess 檔案中
RewriteEngine on
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]
然後可以在 PHP 中使用以下方式取得標頭
<?php
$_SERVER['HTTP_IF_MODIFIED_SINCE'];
$_SERVER['HTTP_IF_NONE_MATCH'];
?>
我已在 Apache/2.2.3/Win32 和 Apache/2.0.54/Unix 上的 PHP/5.1.6 中測試過,它可以完美運作。
注意:如果您已經使用 RewriteRules 來簡化網址,則需要將上述規則放在現有規則的後面。
在所有用於缺少 getallheaders() 的修補程式中使用的全域變數 $_SERVER 只包含真正基本的標頭。要在任何 httpd 環境(包括 CGI/FCGI)中將任何標頭傳遞到 PHP,只需在 .htaccess 中新增規則(任意數量)即可
RewriteRule .* - [E=HTTP_MY_HEADER:%{HTTP:My-Header}]
標頭及其值將以以下方式顯示在 PHP 中
<?php print $_SERVER['HTTP_MY_HEADER']; ?>
而且...就是忍不住。重寫 $_SERVER 鍵值以取代遺失的函式真的不需要正規表達式、preg_matches 或 eval... 试试這個
<?php
函式 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; }
?>
函式 apache_request_headers 在 FCGI PHP-FPM 中不存在
使用這個修補程式:https://gist.github.com/rmpel/11583cfddfcc9705578428e3a2ee3dc1
<?php
// Drop-in replacement for apache_request_headers() when it's not available
if ( ! function_exists( 'apache_request_headers' ) ) {
function apache_request_headers() {
static $arrHttpHeaders;
if ( ! $arrHttpHeaders ) {
// Based on: http://www.iana.org/assignments/message-headers/message-headers.xml#perm-headers
$arrCasedHeaders = array(
// HTTP
'Dasl' => 'DASL',
'Dav' => 'DAV',
'Etag' => 'ETag',
'Mime-Version' => 'MIME-Version',
'Slug' => 'SLUG',
'Te' => 'TE',
'Www-Authenticate' => 'WWW-Authenticate',
// MIME
'Content-Md5' => 'Content-MD5',
'Content-Id' => 'Content-ID',
'Content-Features' => 'Content-features',
);
$arrHttpHeaders = array();
foreach ( $_SERVER as $strKey => $mixValue ) {
if ( 'HTTP_' !== substr( $strKey, 0, 5 ) ) {
continue;
}
$strHeaderKey = strtolower( substr( $strKey, 5 ) );
if ( 0 < substr_count( $strHeaderKey, '_' ) ) {
$arrHeaderKey = explode( '_', $strHeaderKey );
$arrHeaderKey = array_map( 'ucfirst', $arrHeaderKey );
$strHeaderKey = implode( '-', $arrHeaderKey );
} else {
$strHeaderKey = ucfirst( $strHeaderKey );
}
if ( array_key_exists( $strHeaderKey, $arrCasedHeaders ) ) {
$strHeaderKey = $arrCasedHeaders[ $strHeaderKey ];
}
$arrHttpHeaders[ $strHeaderKey ] = $mixValue;
}
/** in case you need authorization and your hosting provider has not fixed this for you:
* VHOST-Config:
* FastCgiExternalServer line needs -pass-header Authorization
*
* .htaccess or VHOST-config file needs:
* SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
* to add the Authorization header to the environment for further processing
*/
if ( ! empty( $arrHttpHeaders['Authorization'] ) ) {
// in case of Authorization, but the values not propagated properly, do so :)
if ( ! isset( $_SERVER['PHP_AUTH_USER'] ) ) {
list( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) = explode( ':', base64_decode( substr( $_SERVER['HTTP_AUTHORIZATION'], 6 ) ) );
}
}
}
return $arrHttpHeaders;
}
// execute now so other scripts have little chance to taint the information in $_SERVER
// the data is cached, so multiple retrievals of the headers will not cause further impact on performance.
apache_request_headers();
}