How can I get PHP to display the headers it receiv

2020-02-09 05:54发布

Are they all stored in $_SERVER? Even custom ones?

标签: php http header
6条回答
再贱就再见
2楼-- · 2020-02-09 06:21

Every HTTP request header field is in $_SERVER (except Cookie) and the key begins with HTTP_. If you’re using Apache, you can also try apache_request_headers.

查看更多
可以哭但决不认输i
3楼-- · 2020-02-09 06:29

You can simply use apache_request_headers() or its alias getallheaders().

Usage: echo json_encode(getallheaders());

If above function does not exist (old PHP or nginx) you can use this as a fallback:

<?php 
if (!function_exists('getallheaders')){ 
    function getallheaders() { 
       $headers = ''; 
       foreach ($_SERVER as $name => $value) { 
            if (substr($name, 0, 5) == 'HTTP_') { 
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
            } 
       } 
       return $headers; 
    } 
}
?>
查看更多
欢心
4楼-- · 2020-02-09 06:30

you can use getallheaders() to get an array of all HTTP headers sent.

$headers =  getallheaders();
foreach($headers as $key=>$val){
  echo $key . ': ' . $val . '<br>';
}
查看更多
对你真心纯属浪费
5楼-- · 2020-02-09 06:34

Look at the $_SERVER variable to see what it contains. The linked manual page has a lot of useful information, but also simply do a var_dump on it to see what's actually in it. Many of the entries will or won't be filled in, depending on what the client decides to do, and odd quirks of PHP. Looking at the one on my local server, there is also a $_SERVER["ALL_HTTP"] entries that just lists them all as a string, but apparently this isn't standard, as it isn't listed on the manual page.

查看更多
【Aperson】
6楼-- · 2020-02-09 06:37

Try this

print_r($_SERVER)

It will list everything within the array

查看更多
三岁会撩人
7楼-- · 2020-02-09 06:39

you can use apache_request_header(); maybe help you.

$headers = apache_request_headers();        
foreach ($headers as $header => $value) {
 echo "<pre>";
 echo "$header : $value";
 echo "</pre>";
}
查看更多
登录 后发表回答