因此,当浏览器向服务器发出 HTTP 请求时,它采用一些 header (get/post、cookies、host、User Agent 等)的形式。有没有一种方法可以在 php 脚本中读取和显示它们?是的,$_GET、$_POST 和 $_COOKIE 都在那里。我正在寻找其余的标题信息。例如http://pgl.yoyo.org/http/browser-headers.php谢谢。
最佳答案
get_headers()功能是你要找的。如引用
get_headers — Fetches all the headers sent by the server in response to a HTTP request
简单的例子:
$url = 'http://www.example.com';
print_r(get_headers($url));
输出服务器发送的所有信息:
Array (
[0] => HTTP/1.1 200 OK [1] => Date: Sat, 29 May 2004 12:28:13 GMT [2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT [4] => ETag: "3f80f-1b6-3e1cb03b" [5] => Accept-Ranges: bytes [6] => Content-Length: 438 [7] => Connection: close [8] => Content-Type: text/html )
更新
要接收浏览器发送的信息,可以从 $_SERVER
访问它们。 super 全局变量。例如,以下代码片段给出了所有浏览器相关信息。
echo $_SERVER['HTTP_USER_AGENT'];
同样,
REQUEST_METHOD
: 给出 HTTP 请求方法,如 GET、HEAD、Put、PostHTTP_HOST
: 给出HOST信息HTTP_COOKIE
:给出关于 Cookie header 的原始信息 [Source]
关于PHP : Show the headers received from the browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12024479/