Learn how to obtain header information in PHP in two minutes

烟雨青岚
Release: 2023-04-09 07:44:01
forward
7400 people have browsed it

Learn how to obtain header information in PHP in two minutes

Researched the problem of custom header information in PHP all night. I have read a lot of code, but I have limited knowledge and skills. Even the test demo didn't work. I'm really ashamed. Here I see a method of obtaining header information. It’s pretty good. Let’s record it.

We can get the header data in $_SERVER. Custom data uses HTTP_ as the prefix, so the data with the HTTP_ prefix can be read out.

public 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;
  }
Copy after login

Reference link: http://blog.csdn.net/fdipzone/article/details/49518535

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/LJFPHP/article/details/78897951

Recommended tutorial: "php tutorial"

The above is the detailed content of Learn how to obtain header information in PHP in two minutes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template