How can I get PHP to display the headers it receiv

2020-02-09 05:48发布

问题:

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

回答1:

Try this

print_r($_SERVER)

It will list everything within the array



回答2:

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

$headers =  getallheaders();
foreach($headers as $key=>$val){
  echo $key . ': ' . $val . '<br>';
}


回答3:

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.



回答4:

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; 
    } 
}
?>


回答5:

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.



回答6:

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>";
}


标签: php http header