一、认识HTTP

        HTTP协议历史及设计思路(点击浏览)

二、获取浏览器HTTP请求header信息

1. Apach服务器下可以直接使用 PHP自带函数获取客户端HTTP请求头信息

  1. /*
  2. 作用:获取客户端HTTP请求所有头信息(header)
  3. 参数:无。
  4. 返回:HTTP请求所有头信息数组
  5. */
  6. getallheaders()

 

 实例:

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

 

==>输出

  1. array(12) {
  2. ["Content-Type"] => string(0) ""
  3. ["Content-Length"] => string(1) "0"
  4. ["X-Original-Url"] => string(21) "/Home/Other/getHeader"
  5. ["Upgrade-Insecure-Requests"] => string(1) "1"
  6. ["User-Agent"] => string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"
  7. ["Host"] => string(17) "www.example.com"
  8. ["Cookie"] => string(36) "PHPSESSID=7rjh2uomb8477dggmr85bg9067"
  9. ["Accept-Language"] => string(14) "zh-CN,zh;q=0.9"
  10. ["Accept-Encoding"] => string(13) "gzip, deflate"
  11. ["Accept"] => string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
  12. ["Connection"] => string(10) "keep-alive"
  13. ["Cache-Control"] => string(9) "max-age=0"
  14. }

 

2.  非apach环境下,可根据服务器$_SERVER信息获取HTTP请求的header信息,设计函数:

  1. function getHeader() {
  2. $headers = array();
  3. foreach ($_SERVER as $key => $value) {
  4. if ('HTTP_' == substr($key, 0, 5)) {
  5. $headers[str_replace('_', '-', substr($key, 5))] = $value;
  6. }
  7. if (isset($_SERVER['PHP_AUTH_DIGEST'])) {
  8. $header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST'];
  9. } elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
  10. $header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']);
  11. }
  12. if (isset($_SERVER['CONTENT_LENGTH'])) {
  13. $header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];
  14. }
  15. if (isset($_SERVER['CONTENT_TYPE'])) {
  16. $header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];
  17. }
  18. }
  19. return $headers;
  20. }

 

实例:

  1. <?php
  2. var_dump(getHeader());

 

==>输出

  1. array(11) {
  2. ["X-ORIGINAL-URL"] => string(21) "/Home/Other/getHeader"
  3. ["UPGRADE-INSECURE-REQUESTS"] => string(1) "1"
  4. ["USER-AGENT"] => string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"
  5. ["HOST"] => string(17) "www.example.com"
  6. ["COOKIE"] => string(36) "PHPSESSID=7rjh2uomb8477dggmr85bg9067"
  7. ["ACCEPT-LANGUAGE"] => string(14) "zh-CN,zh;q=0.9"
  8. ["ACCEPT-ENCODING"] => string(13) "gzip, deflate"
  9. ["ACCEPT"] => string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
  10. ["CONTENT-LENGTH"] => string(1) "0"
  11. ["CONNECTION"] => string(10) "keep-alive"
  12. ["CACHE-CONTROL"] => string(9) "max-age=0"
  13. }

 

三、获取服务器HTTP响应header信息

  1. /*
  2. 作用:获取服务器响应一个 HTTP 请求所发送的所有标头
  3. 参数:
  4. url:目标 URL;
  5. format:如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
  6. 返回:返回包含有服务器响应一个 HTTP 请求所发送标头的索引或关联数组,如果失败则返回 FALSE。
  7. */
  8. get_headers ( string $url [,int $format = 0 ] )

 

实例:

  1. <?php
  2. $url = 'http://www.example.com';
  3. print_r(get_headers($url));
  4. print_r(get_headers($url, 1));
  5. ?>

 

==>输出

  1. Array
  2. (
  3. [0] => HTTP/1.1 200 OK
  4. [1] => Date: Sat, 29 May 2004 12:28:13 GMT
  5. [2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
  6. [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
  7. [4] => ETag: "3f80f-1b6-3e1cb03b"
  8. [5] => Accept-Ranges: bytes
  9. [6] => Content-Length: 438
  10. [7] => Connection: close
  11. [8] => Content-Type: text/html
  12. )
  13. Array
  14. (
  15. [0] => HTTP/1.1 200 OK
  16. [Date] => Sat, 29 May 2004 12:28:14 GMT
  17. [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
  18. [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
  19. [ETag] => "3f80f-1b6-3e1cb03b"
  20. [Accept-Ranges] => bytes
  21. [Content-Length] => 438
  22. [Connection] => close
  23. [Content-Type] => text/html
  24. )

 

四,参考资料

1.  https://www.yiibai.com/manual/php/function.get-headers.html

2.  https://www.oschina.net/question/54100_38761

3.  https://blog.csdn.net/xuezhiwu001/article/details/61203045

【PHP】获取浏览器HTTP请求header信息、获取服务器HTTP响应header信息