Thursday, October 16, 2025
HomeLanguagesPHP | headers_list() Function

PHP | headers_list() Function

The header_list() function is an inbuilt function in PHP which returns the list of response header that sent or ready to send to the client or browser, in the form of array.

Syntax:

array headers_list( void )

Parameters: This function does not accept any parameter.
Return Value: It returns a list or array of headers to be sent to the browser or client.

Note: It differs from headers_sent() function which is suitable to check whether the header sent or not but headers_list() is not favorable in this context as it lists both sent and ready to send states of headers.

Example 1:




<?php
  
// Use setcookie() function to add
// response header
setcookie("cookie1", "value_of_cookie1");
  
// Define the header 
header("test: neveropen");
  
header("Content-type: text/plain");
  
// Display the array returned by the
// headers_list() function
print_r(headers_list());
  
?>


Output:

Array
(   
    [0] => Set-Cookie: cookie1=value_of_cookie1
    [1] => test: neveropen
    [2] => Content-type: text/plain;charset=UTF-8
)

Example 2:




<?php
  
// Use setcookie() function to add 
// header automatically
setcookie('uid', 'user4059');
  
// Defining a custom response header
header("custom-res-header: cstm");
  
// Specifying plain text content in
// response
header('Content-type: text/plain');
  
// Display all sent headers
var_dump(headers_list());
  
?>


Output:

array(3) {
  [0]=>string(24) "Set-Cookie: uid=user4059"
  [1]=>string(23) "custom-res-header: cstm"
  [2]=>string(38) "Content-type: text/plain;charset=UTF-8"
}

Reference: http://php.net/manual/en/function.headers-list.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS