我剛將腳本切換到不同的服務器。在以前的服務器上,這個工作完美無瑕,現在我已經將它們切換到不同的服務器,我無法理解這個問題。調用未定義的函數apache_request_headers()
我不確定這會有幫助,但這裏是相關的代碼。
$headers = apache_request_headers();
PHP版本:PHP 5.3.2
任何幫助,將不勝感激。
我剛將腳本切換到不同的服務器。在以前的服務器上,這個工作完美無瑕,現在我已經將它們切換到不同的服務器,我無法理解這個問題。調用未定義的函數apache_request_headers()
我不確定這會有幫助,但這裏是相關的代碼。
$headers = apache_request_headers();
PHP版本:PHP 5.3.2
任何幫助,將不勝感激。
從docs,PHP 5.4.0之前發佈:
當PHP安裝爲Apache模塊只支持此功能。
PHP 5.4.0及更高版本無條件支持此功能。
所述文檔還包括通過步進$_SERVER
來模擬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();
// 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 Manual
效果不錯! – Zabs 2012-11-05 16:23:49
我在我的網站上使用過這個功能,但它似乎運行緩慢,有什麼方法可以加速嗎? – Zabs 2012-11-06 11:30:14
@Zabs:是的,你可以在這個函數中設置'$ arh'作爲一個靜態變量來確保它只運行一次。 [示例](http://pastebin.com/2NGARe49) – machineaddict 2013-10-02 09:20:23
如the other answer here的建議,我已經使用從PHP documentation註釋的功能,卻發現它是最理想的,硬閱讀/維護,而不是完整的(與一些標題的(不符合)的外殼相比)。
,是因爲我需要真的能夠依靠它,我重新編碼它是更加明顯,處理更多的邊緣的情況下更好地以及 - 原代碼甚至指出「做一些討厭的字符串操作來恢復原信件案例「和」這應該在大多數情況下工作「,這聽起來不太好,你應該能夠依靠。
這並不完美,但我發現它更可靠。它缺乏的一件事是處理實際的或原始的頭文件,因爲對輸出結果將反映對$_SERVER
的任何修改。這可以通過將結果設爲靜態並在每個請求中首先運行函數來緩解。
<?php
// Drop-in replacement for apache_request_headers() when it's not available
if(!function_exists('apache_request_headers')) {
function apache_request_headers() {
// 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;
}
return $arrHttpHeaders;
}
}
如果PHP安裝爲Apache模塊:
apache_request_headers()["Authorization"];
否則,轉到。htaccess的文件,並添加:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
然後,您可以訪問使用任何這些請求頭:
$_SERVER["HTTP_AUTHORIZATION"]; // using super global
OR
使用「apache_request_headers時$app->request->headers("Authorization"); // if using PHP Slim
這對我有用 – adrian4aes 2015-04-24 22:22:54
工作正常以獲得授權,謝謝:) – Tiger 2016-03-17 05:22:29
需要.htaccess和@Babatunde Adeyemi的功能。謝謝! – 2016-11-16 23:02:19
同樣的事情發生在我身上() 「所以我用這個代碼 - 完美地爲我輸出所有的標題:
<?php
$headers = array();
foreach($_SERVER as $key => $value) {
if(strpos($key, 'HTTP_') === 0) {
$headers = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
echo $headers." : ". $i[$headers] = $value . "<br>";
}
}
?>
輸出例如:
Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding : gzip, deflate
Accept-Language : en-US,en;q=0.5
Cache-Control : max-age=0
Connection : keep-alive
Host : example.com
Referer : https://example.com/
User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
如果使用此函數,我無法獲得授權標頭 – adrian4aes 2015-04-24 22:15:13
爲什麼在回顯它之前將值賦給$ i [$ headers]? – 2018-01-19 12:21:41
它爲我工作,謝謝..我趕上if($我[$頭] ==「授權」){ – 2018-03-05 06:35:26
[當前文檔](http://www.php.net/manual/en/intro.apache.php)仍然提到'阿帕奇_ *()'函數是僅在作爲Apache模塊運行PHP時可用。 'apache_request_headers()'的[specific doc](http://www.php.net/manual/en/function.apache-request-headers.php)表示現在(> = 5.4.0)在FastCGI下可用,但我懷疑它可以*無條件*,例如在IIS下? – Benjamin 2014-06-27 13:41:54