Current Location: Home> Latest Articles> How to Use headers_list() to View the List of Currently Set HTTP Headers?

How to Use headers_list() to View the List of Currently Set HTTP Headers?

M66 2025-07-04

In PHP, the headers_list() function is a very useful tool for viewing all the HTTP headers that have been set in the current script. It returns an array containing the names and corresponding values of all HTTP headers. This is especially important for debugging HTTP header settings, particularly when checking whether certain headers have been set correctly before they are sent.

Basic Usage

The headers_list() function does not accept any parameters. When called, it returns an array containing all the HTTP headers that have been sent. Here is a basic example demonstrating how to use the headers_list() function:

<?php
// Set some HTTP headers
header('Content-Type: application/json');
header('Cache-Control: no-cache');
<p>// Get all the currently set HTTP headers<br>
$headers = headers_list();</p>
<p>// Output all the headers<br>
foreach ($headers as $header) {<br>
echo $header . "<br>";<br>
}<br>
?><br>

In the example above, we first use the header() function to set two HTTP headers: Content-Type and Cache-Control. Then, by calling headers_list(), we retrieve all the HTTP headers that have been set and output them one by one.

Output Example

When you run the code above, the output might look something like this:

Content-Type: application/json
Cache-Control: no-cache

Notes

  1. Order of Header Information: The headers returned by headers_list() are arranged in the order in which they were sent. This means that if you have set multiple headers of the same type in the script, headers_list() will display those headers, and the order will reflect the sequence in which they were set.

  2. Must be Called Before Output: It is important to note that headers_list() can only be called before any output is sent. PHP sends HTTP headers before any content is sent to the browser. Therefore, if any output (such as echo or HTML code) has already been sent before calling headers_list(), you will not be able to retrieve the headers.

Setting Custom HTTP Headers

If you need to set custom HTTP headers and view them, you can use code like the following:

<?php
// Set a custom HTTP header
header('X-Custom-Header: m66.net');
<p>// Get the current HTTP headers<br>
$headers = headers_list();</p>
<p>// Output all the headers<br>
foreach ($headers as $header) {<br>
echo $header . "<br>";<br>
}<br>
?><br>

In this example, we set a custom header named X-Custom-Header with the value m66.net. By calling headers_list(), we can check if it has been successfully set.

Other Related Functions

In addition to headers_list(), PHP provides other related functions that can help you manipulate and manage HTTP headers:

  • header(): Used to send a raw HTTP header to the browser.

  • headers_sent(): Checks if HTTP headers have already been sent.

By combining these functions, you can flexibly control the behavior of HTTP headers and ensure that data is correctly transmitted to the client.