如何读取 PHP 中的请求标头?
我应该如何读取 PHP 中的任何标头?
例如自定义标头:X-Requested-With
。
How should I read any header in PHP?
For example the custom header: X-Requested-With
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
IF:您只需要一个标头,而不是所有标头,最快的方法是:
ELSE IF:将 PHP 作为 Apache 模块运行,或者从 PHP 5.4 开始,使用 FastCGI(简单方法):
apache_request_headers()
ELSE:在任何其他情况下,您可以使用(用户态实现):
另请参阅:
getallheaders() - (PHP >= 5.4)
跨平台版本apache_request_headers()
的别名apache_response_headers() - 获取所有 HTTP 响应标头。< br>
headers_list() - 获取要发送的标头列表。
IF: you only need a single header, instead of all headers, the quickest method is:
ELSE IF: you run PHP as an Apache module or, as of PHP 5.4, using FastCGI (simple method):
apache_request_headers()
ELSE: In any other case, you can use (userland implementation):
See Also:
getallheaders() - (PHP >= 5.4)
cross platform editionAlias ofapache_request_headers()
apache_response_headers() - Fetch all HTTP response headers.
headers_list() - Fetch a list of headers to be sent.
RFC3875,4.1.18:
RFC3875, 4.1.18:
您应该在
$_SERVER
全局变量中找到所有 HTTP 标头,其前缀为大写的HTTP_
,并且破折号 (-) 替换为下划线 (_)。例如,您的
X-Requested-With
可以在以下位置找到:从
$_SERVER
变量创建关联数组可能会很方便。 这可以通过多种样式完成,但这里有一个输出驼峰键的函数:现在只需使用
$headers['XRequestedWith']
来检索所需的标头。$_SERVER
上的 PHP 手册:http://php. net/manual/en/reserved.variables.server.phpYou should find all HTTP headers in the
$_SERVER
global variable prefixed withHTTP_
uppercased and with dashes (-) replaced by underscores (_).For instance your
X-Requested-With
can be found in:It might be convenient to create an associative array from the
$_SERVER
variable. This can be done in several styles, but here's a function that outputs camelcased keys:Now just use
$headers['XRequestedWith']
to retrieve the desired header.PHP manual on
$_SERVER
: http://php.net/manual/en/reserved.variables.server.php从 PHP 5.4.0 开始,您可以使用
getallheaders
函数它以关联数组的形式返回所有请求标头:此前,此函数仅在 PHP 作为 Apache/NSAPI 模块运行时才起作用。
Since PHP 5.4.0 you can use
getallheaders
function which returns all request headers as an associative array:Earlier this function worked only when PHP was running as an Apache/NSAPI module.
将标头名称传递给此函数以获取其值,而无需使用
for
循环。 如果未找到标头,则返回 null。注意:这仅适用于 Apache 服务器,请参阅:http://php.ini。 net/manual/en/function.getallheaders.php
注意:此函数将处理所有标头并将其加载到内存中,并且其性能低于
for
循环。Pass a header name to this function to get its value without using
for
loop. Returns null if header not found.Note: this works only with Apache server, see: http://php.net/manual/en/function.getallheaders.php
Note: this function will process and load all of the headers to the memory and it's less performant than a
for
loop.我正在使用 CodeIgniter 并使用下面的代码来获取它。 可能对将来的某人有用。
I was using CodeIgniter and used the code below to get it. May be useful for someone in future.
几个提议的解决方案中缺少
strtolower
,RFC2616 (HTTP/1.1) 将标头字段定义为不区分大小写的实体。 整个事情,而不仅仅是价值部分。因此,诸如仅解析 HTTP_ 条目之类的建议是错误的。
更好的是这样的:
注意与之前建议的细微差别。 这里的功能也适用于 php-fpm (+nginx)。
strtolower
is lacking in several of the proposed solutions, RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. The whole thing, not just the value part.So suggestions like only parsing HTTP_ entries are wrong.
Better would be like this:
Notice the subtle differences with previous suggestions. The function here also works on php-fpm (+nginx).
如果只需要检索一个密钥,例如需要
"Host"
地址,那么我们可以使用这样我们就可以避免循环并将其内联到回显输出
if only one key is required to retrieved, For example
"Host"
address is required, then we can useSo that we can avoid loops and put it inline to the echo outputs
为了简单起见,以下是如何获取您想要的:
简单:
或者当您需要一次获取一个时:
其他标头也在超级全局数组 $_SERVER 中,您可以阅读如何获取在这里: http://php.net/manual/en/reserved.variables .server.php
To make things simple, here is how you can get just the one you want:
Simple:
or when you need to get one at a time:
The other headers are also in the super global array $_SERVER, you can read about how to get at them here: http://php.net/manual/en/reserved.variables.server.php
如果您有 Apache 服务器
PHP 代码:
结果:
This work if you have an Apache server
PHP Code:
Result:
我是这样做的。 如果未传递 $header_name ,您需要获取所有标头:
对我来说,它看起来比其他答案中给出的大多数示例简单得多。 这还会获取方法(GET/POST/等)以及获取所有标头时请求的 URI,如果您尝试在日志记录中使用它,这可能会很有用。
这是输出:
Here's how I'm doing it. You need to get all headers if $header_name isn't passed:
It looks a lot simpler to me than most of the examples given in other answers. This also gets the method (GET/POST/etc.) and the URI requested when getting all of the headers which can be useful if you're trying to use it in logging.
Here's the output:
这是一个简单的方法。
Here is an easy way to do it.
这个 PHP 小片段会对您有所帮助:
This small PHP snippet can be helpful to you:
我使用此函数来获取自定义标头,如果标头从“HTTP_X_”开始,我们将推入数组:)
I use this function to get the custom headers, if the header starts from "HTTP_X_" we push in the array :)
PHP 7:空合并运算符
PHP 7: Null Coalesce Operator
下面的代码适用于我获取标头中提交的任何特定数据
Below code works for me to get any specific data submitted in the header