PHP手册提供了现成的函数:

getallheaders

(PHP 4, PHP 5)

getallheaders — Fetch all HTTP request headers

说明

array
getallheaders ( void )

Fetches all HTTP headers from the current request.

This function is an alias for apache_request_headers(). Please read theapache_request_headers() documentation for more
information on how this function works.

返回值

An associative array of all the HTTP headers in the current request, orFALSE on failure.

Example #1 getallheaders() example


  1. <?php
  2. foreach (getallheaders() as $name => $value) {
  3. echo "$name: $value\n";
  4. }
  5. ?>

不过这个函数只能在apache环境下使用,iis或者nginx并不支持,可以通过自定义函数实现

  1. <?php
  2. <span class="html">if (!function_exists('getallheaders'))
  3. {
  4. function getallheaders()
  5. {
  6. foreach ($_SERVER as $name => $value)
  7. {
  8. if (substr($name, 0, 5) == 'HTTP_')
  9. {
  10. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  11. }
  12. }
  13. return $headers;
  14. }
  15. }</span>
  16. ?>

好了,看看都打印出了啥吧

  1. <?php
  2. print_r(getallheaders());

获得结果:

    1. Array
    2. (
    3. [Accept] => */*
    4. [Accept-Language] => zh-cn
    5. [Accept-Encoding] => gzip, deflate
    6. [User-Agent] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
    7. [Host] => localhost
    8. [Connection] => Keep-Alive
    9. )

PHP获取http头信息的更多相关文章

  1. PHP获取http头信息和CI中获取HTTP头信息的方法

    CI中获取HTTP头信息的方法: $this->input->request_headers() 在不支持apache_request_headers()的非Apache环境非常有用.返回 ...

  2. js获取response头信息

    当我们使用ajax发起请求时,经常需要获取请求返回的头信息.默认情况下,js货可以获取如下头信息: Cache-Control Content-Language Content-Type Expirs ...

  3. Ajax获取Response头信息

    $.ajax({ type: 'HEAD', // 获取头信息,type=HEAD即可 url : window.location.href, complete: function( xhr,data ...

  4. javascript 获取http头信息

    Javascript中跟response header有关的就两个方法: getResponseHeader 从响应信息中获取指定的http头 语法 strValue = oXMLHttpReques ...

  5. jsp获取请求头信息

    <%@ page language="java" import="java.util.*" contentType="text/html; ch ...

  6. 获取相应头信息Response Headers的内容类型Content-Type

    Content-Type内容类型,也是键值对的形式: 我们在代码中获取: package com.zhi.httpClient2; import org.apache.http.Header; imp ...

  7. springmvc 获取请求头信息

    @PostMapping("/test/post") public void post(@RequestBody String params, @RequestHeader(req ...

  8. 动手学servlet(三) 请求头和响应头信息

    获取请求头信息 package servletdemo; import java.io.IOException; import java.util.Enumeration; import javax. ...

  9. JAVA Serverlet 请求头信息和响应头信息

    <1>获取请求头信息 //获取请求头信息的全部名称 Enumeration<String> header = request.getHeaderNames(); while(h ...

随机推荐

  1. HashRouter与BrowserRouter的异同

    项目中控制路由跳转使用的是BrowserRouter 在开发过程中使用是没有问题的,但是将页面上传至服务器之后,问题就来了:用户访问的资源不存在,页面是空白的. 原因: 在browserHistory ...

  2. 第一步 使用sencha touch cmd 4.0 创建项目、打包(加入全局变量、公用类、自定义扩展、资源文件)

    参考资料: http://www.cnblogs.com/qqloving/archive/2013/04/25/3043606.html http://www.admin10000.com/docu ...

  3. Linux系统更改网卡名称

    自己装了一台机器,有两张网卡,一个是主板上自带的,还有一个是后来自己添加的.装完系统后,系统默认主板上的网卡为eth1,而自己添加的网卡是eth0,感觉不爽,所以想办法使用udev使系统将主板上的网卡 ...

  4. Rails: could not connect to database postgres: FATAL: Peer authentication failed for user "username"

    /var/lib/pgsql/9.2/data/pg_hba.conf 打开之后找到 local all postgres/all peer 改成 local all postgres trust 保 ...

  5. C# IEqualityComparer 去重

    1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...

  6. Spring Cloud Eureka 配置

    实例名配置       在Netflix Eureka的原生实现中,实例名采用主机名作为默认值,这样的设置使得在同一主机上无法启动多个相同的实例,所以在Spring Cloud Eureka的配置中, ...

  7. vim配置函数跳转(c/c++)

    暂时草记一下,有时间好好整理 ctags 如果只是查看函数与变量是在哪里定义的,用ctags就可以了. ctrl+]跳到定义的地方,ctrl+t跳回来. 想要像IDE那样在旁边显示函数与变量列表,用t ...

  8. openstack 部署(Q版)-----环境准备篇

    一.环境准备 系统:centos7 cinder01 内网:192.168.10.51 外网:172.16.1.51 compute01   内网:192.168.10.52 外网:172.16.1. ...

  9. 洛谷P2216 理想的正方形

    题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...

  10. codeforces 894C - Marco and GCD Sequence - [有关gcd数学题]

    题目链接:https://cn.vjudge.net/problem/CodeForces-894C In a dream Marco met an elderly man with a pair o ...