首页 > 文章列表 > 在 PHP 中获取标头

在 PHP 中获取标头

PHP编程 后端开发
105 2024-03-09

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