Home > Article > Backend Development > 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; }
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!