Home  >  Article  >  php教程  >  PHP getallheaders无法获取自定义头(headers)的问题,phpgetallheaders

PHP getallheaders无法获取自定义头(headers)的问题,phpgetallheaders

WBOY
WBOYOriginal
2016-06-13 08:43:371161browse

PHP getallheaders无法获取自定义头(headers)的问题,phpgetallheaders

在客户端请求的时候增加了自定义的http头,请求如下所示:

 

自定义http请求头

var_dump(getallheaders);

一开始通过getallheaders参数获取,但是发现在nginx部署的服务器上获取不到,非常奇怪,查看php手册发现getallheaders这个函数只支持apache服务器。于是找到兼容的方法:

if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = array();
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
var_dump(getallheaders());

实际上这个方法就是找到$_SERVER变量中以HTTP_开头的属性,对属性做一个字符串替换这样。$_SERVER变量中的HTTP_USER_ID实际就是上面自定义的User-Id:

 

php中$_SERVER变量

另外关于自定义Http头, 需要注意头的命名规范,不能用下划线命名 ,否则在nginx服务器下读取不到,在查找命名规范的时候,有提到自定义属性用X-开头这个问题。后来查阅了一些资料,发现后来的http协议不建议这样去做。

以上内容是针对PHP getallheaders无法获取自定义头(headers)的问题的相关叙述,希望对大家有所帮助!

您可能感兴趣的文章:

  • php session_start()关于Cannot send session cache limiter - headers already sent错误解决方法
  • php 模拟get_headers函数的代码示例
  • 使用php get_headers 判断URL是否有效的解决办法
  • php中get_headers函数的作用及用法的详细介绍
  • PHP提示Cannot modify header information - headers already sent by解决方法
  • PHP错误Warning: Cannot modify header information - headers already sent by解决方法
  • PHP使用get_headers函数判断远程文件是否存在的方法
  • PHP实现的带超时功能get_headers函数
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn