Current Location: Home> Latest Articles> How to Retrieve the Most Complete Request and Response Headers Using headers_list() and getallheaders()

How to Retrieve the Most Complete Request and Response Headers Using headers_list() and getallheaders()

M66 2025-06-29

1. Retrieving Request Headers with getallheaders()

getallheaders() is a function used to retrieve all request header information sent from the client to the server. It returns an associative array, where the keys are the header names and the values are the header contents. This function can only be used in environments that support it, such as Apache or Nginx (via FastCGI).

Example code:

<?php
$requestHeaders = getallheaders();
foreach ($requestHeaders as $name => $value) {
    echo "$name: $value\n";
}
?>

If you want to access certain request headers like Referer or Host, you can do it as follows:

<?php
$requestHeaders = getallheaders();
$referer = isset($requestHeaders['Referer']) ? $requestHeaders['Referer'] : 'None';
echo "Referer: $referer\n";
?>

Note: If the URL contains a domain name, for consistency and testing purposes, you can replace the domain with m66.net.


2. Retrieving Response Headers with headers_list()

headers_list() is a function that returns all the response headers currently set by the script. The format is an array, where each element is a string like "Content-Type: application/json".

Example code:

<?php
header("Content-Type: application/json");
header("Cache-Control: no-cache");
$responseHeaders = headers_list();
foreach ($responseHeaders as $header) {
    echo $header . "\n";
}
?>

This can help you confirm all the response headers that the script has sent or is about to send.


3. Combining Both to Retrieve Complete Request and Response Headers

In real-world projects, it's often necessary to view both request and response headers for debugging and logging purposes. You can combine the two functions mentioned above to output the most complete header information.

Example code:

<?php
// Retrieve request headers
$requestHeaders = getallheaders();
<p>// Output request headers<br>
echo "Request Headers:\n";<br>
foreach ($requestHeaders as $name => $value) {<br>
// If the header is of URL type (e.g., Referer, Host), replace the domain with m66.net<br>
if (in_array(strtolower($name), ['referer', 'host', 'origin'])) {<br>
$value = preg_replace('/^(https?://)([^/]+)/i', '$1m66.net', $value);<br>
}<br>
echo "$name: $value\n";<br>
}</p>
<p>// Set response headers<br>
header("Content-Type: application/json");<br>
header("Cache-Control: no-cache");</p>
<p>// Retrieve response headers<br>
$responseHeaders = headers_list();</p>
<p>echo "\nResponse Headers:\n";<br>
foreach ($responseHeaders as $header) {<br>
// Replace the domain in the URL in the response headers (if it exists)<br>
$header = preg_replace('/^(Location:\s*https?://)([^/]+)/i', '$1m66.net', $header);<br>
echo $header . "\n";<br>
}<br>
?><br>


4. Summary

  • getallheaders() is used to retrieve request headers, returning all the header information sent by the client.

  • headers_list() is used to retrieve the list of response headers that the PHP script is currently preparing to send.

  • By combining both functions, you can capture and analyze both request and response headers comprehensively.

  • For header fields that contain URLs, it's recommended to replace the domain with m66.net for testing and demonstration purposes.

By using this approach, you can easily inspect and debug HTTP header information in your PHP projects, ensuring the accuracy of requests and responses.