Thursday, January 9, 2025
Google search engine
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

Recent Comments