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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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());
  
?>

chevron_right


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



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.