PHP Conference Japan 2024

apache_response_headers

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

apache_response_headers擷取所有 HTTP 回應標頭

說明

apache_response_headers(): 陣列

擷取所有 HTTP 回應標頭。適用於 Apache、FastCGI、CLI 和 FPM 網頁伺服器。

參數

此函式沒有參數。

回傳值

成功時,會傳回所有 Apache 回應標頭的陣列。

範例

範例 #1 apache_response_headers() 範例

<?php
print_r
(apache_response_headers());
?>

上述範例將輸出類似以下的內容

Array
(
    [Accept-Ranges] => bytes
    [X-Powered-By] => PHP/4.3.8
)

參見

新增筆記

使用者貢獻的筆記 6 則筆記

Isaac Z dot Schlueter i at foohack dot com
15 年前
lighttpd 上沒有這個函式,所以我寫了這個小函式來模擬它

<?php

if (!function_exists('apache_response_headers')) {
function
apache_response_headers () {
$arh = array();
$headers = headers_list();
foreach (
$headers as $header) {
$header = explode(":", $header);
$arh[array_shift($header)] = trim(implode(":", $header));
}
return
$arh;
}
}

?>
php at mailplus dot pl
13 年前
我在使用 php-cli 時收到「Call to undefined function apache_response_headers()」的錯誤訊息,因此請注意 CLI 缺少此函式。
在我的例子中,我試圖透過 phpunit(當然是命令列工具;))測試我的應用程式是否發送了正確的標頭。
orange
19 年前
如果 apache_response_headers() 返回一個空陣列,請嘗試先呼叫 flush(),它就會被填滿。
athlet
18 年前
為了讓 apache_response_headers() 正常運作,您需要在 php.ini 中設定 output_buffering = Off。
Daniel Lorch
17 年前
補充:程式碼

<pre>
<?php
print_r
(apache_request_headers());
?>
</pre>

給我

陣列
(
[X-Powered-By] => PHP/5.1.6
)

但程式碼
<pre>
<?php
ob_end_flush
();
print_r(apache_request_headers());
?>
</pre>

結果為

陣列
(
[Content-Location] => phpinfo.de.php
[Vary] => negotiate
[TCN] => choice
[X-Powered-By] => PHP/5.1.6
[Keep-Alive] => timeout=15, max=96
[Connection] => Keep-Alive
[Transfer-Encoding] => chunked
[內容類型] => text/html
[內容語言] => 德文
)
Daniel Lorch
17 年前
我可以證實 athlet 使用 PHP 5.1.6 時,在 `apache_response_headers()` 函式上遇到的狀況。當結果陣列為空,或只包含 "X-Powered-By" 而不是完整的標頭列表時,您需要在腳本執行_之前_關閉輸出緩衝,例如在 .htaccess 中使用以下指令:

php_value "output_buffering" "0"

在腳本中使用 `ini_set('output_buffering', 0);` 是_無效的_。我不確定原因為何 - 這並非 PHP 的錯誤,但可能與 Apache 填充請求值的方式有關。
To Top