Home » Php » http – How can I get PHP to display the headers it received from a browser?

http – How can I get PHP to display the headers it received from a browser?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&Answers:

Try this

print_r($_SERVER)

It will list everything within the array

Answer:

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

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

Answer:

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.

Answer:

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

Answer:

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.

Answer:

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