zoukankan      html  css  js  c++  java
  • 获取请求header的各种方法

    部分代码非原创 

    不定期更新

    PHP

     1 function get_all_header() {
     2     // 忽略获取的header数据。这个函数后面会用到。主要是起过滤作用
     3     $ignore = array('host','accept','content-length','content-type');
     4     $headers = array();
     5     //这里大家有兴趣的话,可以打印一下。会出来很多的header头信息。咱们想要的部分,都是‘http_'开头的。所以下面会进行过滤输出。
     6     foreach($_SERVER as $key=>$value){
     7         if(substr($key, 0, 5)==='HTTP_'){
     8             //这里取到的都是'http_'开头的数据。
     9             //前去开头的前5位
    10             $key = substr($key, 5);
    11             //把$key中的'_'下划线都替换为空字符串
    12             $key = str_replace('_', ' ', $key);
    13             //再把$key中的空字符串替换成‘-’
    14             $key = str_replace(' ', '-', $key);
    15             //把$key中的所有字符转换为小写
    16             $key = strtolower($key);
    17             //这里主要是过滤上面写的$ignore数组中的数据
    18             if(!in_array($key, $ignore)){
    19                 $headers[$key] = $value;
    20             }
    21         }
    22     }
    23     //输出获取到的header
    24     return $headers;
    25 }

    jQuery.ajax

     1 $.ajax({
     2     type: 'HEAD', // 获取头信息,type=HEAD即可
     3     url : window.location.href,
     4     complete: function( xhr,data ){
     5         // 获取相关Http Response header
     6         var wpoInfo = {
     7             // 服务器端时间
     8             "date" : xhr.getResponseHeader('Date'),
     9             // 如果开启了gzip,会返回这个东西
    10             "contentEncoding" : xhr.getResponseHeader('Content-Encoding'),
    11             // keep-alive ? close?
    12             "connection" : xhr.getResponseHeader('Connection'),
    13             // 响应长度
    14             "contentLength" : xhr.getResponseHeader('Content-Length'),
    15             // 服务器类型,apache?lighttpd?
    16             "server" : xhr.getResponseHeader('Server'),
    17             "vary" : xhr.getResponseHeader('Vary'),
    18             "transferEncoding" : xhr.getResponseHeader('Transfer-Encoding'),
    19             // text/html ? text/xml?
    20             "contentType" : xhr.getResponseHeader('Content-Type'),
    21             "cacheControl" : xhr.getResponseHeader('Cache-Control'),
    22             // 生命周期?
    23             "exprires" : xhr.getResponseHeader('Exprires'),
    24             "lastModified" : xhr.getResponseHeader('Last-Modified')
    25         };
    26         // 在这里,做想做的事。。。
    27     }
    28 });

    function get_all_header() {    // 忽略获取的header数据。这个函数后面会用到。主要是起过滤作用    $ignore = array('host','accept','content-length','content-type');    $headers = array();    //这里大家有兴趣的话,可以打印一下。会出来很多的header头信息。咱们想要的部分,都是‘http_'开头的。所以下面会进行过滤输出。
        /*    var_dump($_SERVER);    exit;    */        foreach($_SERVER as $key=>$value){        if(substr($key, 0, 5)==='HTTP_'){            //这里取到的都是'http_'开头的数据。            //前去开头的前5位            $key = substr($key, 5);            //把$key中的'_'下划线都替换为空字符串            $key = str_replace('_', ' ', $key);            //再把$key中的空字符串替换成‘-’            $key = str_replace(' ', '-', $key);            //把$key中的所有字符转换为小写            $key = strtolower($key);            //这里主要是过滤上面写的$ignore数组中的数据            if(!in_array($key, $ignore)){                $headers[$key] = $value;            }        }    }    //输出获取到的header    return $headers;}

  • 相关阅读:
    条件变量:为什么要与互斥锁配套使用?为什么要使用while来避免虚假唤醒?
    【转】高性能IO之Reactor模式
    LeetCode127:单词接龙
    CF1245F: Daniel and Spring Cleaning
    权值线段树学习笔记
    luogu_4317: 花神的数论题
    luogu_2605: 基站选址
    入门平衡树: Treap
    CF1244C: The Football Season
    luogu_1156: 垃圾陷阱
  • 原文地址:https://www.cnblogs.com/Dota-wiki/p/9385386.html
Copyright © 2011-2022 走看看