HOWTO · PHP
在 PHP 中獲取標頭
本教程演示如何獲取伺服器傳送的響應 HTTP 請求的標頭。
本頁內容
HTTP 標頭在 Web 伺服器和瀏覽器之間傳輸資料以進行通訊。
每當我們在瀏覽器的位址列中輸入一個 URL 時,它就會向伺服器傳送一個 HTTP 請求;它包含一個標題。
在 PHP 中使用 get_headers() 獲取給定 URL 的標頭
get_headers() 是 PHP 內建函式,用於獲取伺服器響應 HTTP 請求而傳送的標頭。
<?php
$URL = 'https://www.delftstack.com/';
$headers = get_headers($URL);
foreach($headers as $value) {
echo $value;
echo "<br>";
}
?>
上面的程式碼獲取伺服器為 https://www.delftstack.com/ 傳送的所有標頭。
輸出:
HTTP/1.0 200 OK
Age: 0
Cache-Control: max-age=2592000, private, s-maxage=0, proxy-revalidate
Content-Type: text/html; charset=UTF-8
Date: Fri, 25 Feb 2022 12:00:31 GMT
Display: pub_site_to_orig_sol
Etag: "6b7e22637c1ca646a2c1db6894a4b0f8-ssl-df-gzip"
Pagespeed: off
Response: 200
Server: nginx
Set-Cookie: ezoadgid_96282=-1; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Set-Cookie: ezoref_96282=; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 14:00:31 UTC
Set-Cookie: ezoab_96282=mod1; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 14:00:31 UTC
Set-Cookie: active_template::96282=pub_site.1645790431; Path=/; Domain=delftstack.com; Expires=Sun, 27 Feb 2022 12:00:31 UTC
Set-Cookie: lp_96282=https://www.delftstack.com/; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Set-Cookie: ezovuuidtime_96282=1645790431; Path=/; Domain=delftstack.com; Expires=Sun, 27 Feb 2022 12:00:31 UTC
Set-Cookie: ezovuuid_96282=23606c54-6e8e-42a1-4745-bf6c0d668d64; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Set-Cookie: ezopvc_96282=1; Path=/; Domain=delftstack.com; Expires=Fri, 25 Feb 2022 12:30:31 UTC
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
Vary: Accept-Encoding,User-Agent
X-Ezoic-Cdn: Hit ds;mm;ba60b18a465a11ac0d2ea4d2e9f91570;2-96282-33;ec6a48c1-7683-4a48-429a-88731467543c
X-Middleton-Display: pub_site_to_orig_sol
X-Middleton-Response: 200
X-Nf-Request-Id: 01FWR6BN2347S6Y8M9PW8W70CW
X-Origin-Cache-Control: public, max-age=0, must-revalidate
X-Sol: pub_site
在 PHP 中使用 $_SERVER 為你的伺服器獲取單個 HTTP 請求標頭
我們的 localhost 伺服器包含 $_SERVER 陣列中的所有標頭資訊。我們可以通過放置特定的索引名稱來獲取單個 HTTP 請求標頭的資訊。
<?php
//print_r($_SERVER);
echo $_SERVER['HTTP_HOST']."<br>";
echo $_SERVER['HTTP_USER_AGENT']."<br>";
echo $_SERVER['HTTP_CONNECTION'];
?>
上面的程式碼獲取給定單個 HTTP 請求的資訊。
輸出:
localhost
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
keep-alive
在 PHP 中使用 apache_request_headers() 函式獲取 Apache 伺服器的所有請求標頭
PHP 中的 apache_request_headers() 內建函式用於獲取 apache 模組的標頭。
<?php
$apache_headers= apache_request_headers();
foreach ($apache_headers as $key => $value) {
echo "$key => $value <br/>";
}
?>
輸出將顯示 apache 模組的 HTTP 資訊:
Host => localhost
User-Agent => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Accept => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language => en-US,en;q=0.5
Accept-Encoding => gzip, deflate
Connection => keep-alive
Upgrade-Insecure-Requests => 1
Sec-Fetch-Dest => document
Sec-Fetch-Mode => navigate
Sec-Fetch-Site => none
Sec-Fetch-User => ?1
getallheaders() 是 apache_request_headers 的更新版本,工作方式類似。
在 PHP 中使用 $_SERVER 獲取伺服器的所有 HTTP 請求標頭
$_SERVER 包含很多 HTTP 請求頭以外的資訊。
<?php
function get_HTTP_request_headers() {
$HTTP_headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$single_header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$HTTP_headers[$single_header] = $value;
}
return $HTTP_headers;
}
$headers = get_HTTP_request_headers();
foreach ($headers as $key => $value) {
echo "$key => $value <br/>";
}
?>
上面的程式碼將從 $_SERVER 陣列中提取 HTTP 請求標頭。
輸出:
Host => localhost
User-Agent => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Accept => text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language => en-US,en;q=0.5
Accept-Encoding => gzip, deflate
Connection => keep-alive
Upgrade-Insecure-Requests => 1
Sec-Fetch-Dest => document
Sec-Fetch-Mode => navigate
Sec-Fetch-Site => none
Sec-Fetch-User => ?1