PHP 8.5.0 Alpha 1 available for testing

headers_list

(PHP 5, PHP 7, PHP 8)

headers_listDevuelve la lista de los encabezados de respuesta del script actual

Descripción

headers_list(): array

headers_list() devuelve un array con la lista de los encabezados que serán transmitidos al navegador. Para determinar si estos encabezados han sido ya enviados o no, utilice la función headers_sent().

Parámetros

Esta función no tiene parámetros.

Valores devueltos

Devuelve un array de encabezados indexado numéricamente.

Ejemplos

Ejemplo #1 Ejemplo con headers_list()

<?php

/* setcookie() va añadir un encabezado */
setcookie('foo', 'bar');

/* Define un encabezado de respuesta
Será ignorado por la mayoría de los navegadores */
header("Example-Test: foo");

/* Especificación de la respuesta en texto simple */
header('Content-Type: text/plain; charset=UTF-8');

/* ¿Cuáles son los encabezados que serán enviados? */
var_dump(headers_list());

?>

El resultado del ejemplo sería algo similar a:

array(4) {
  [0]=>
  string(19) "Set-Cookie: foo=bar"
  [1]=>
  string(17) "Example-Test: foo"
  [2]=>
  string(39) "Content-Type: text/plain; charset=UTF-8"
}

Notas

Nota:

Las cabeceras sólo serán accesibles y mostradas si se utiliza un SAPI que lo soporte.

Ver también

add a note

User Contributed Notes 2 notes

up
22
Anonymous
12 years ago
note that it does not return the status header

<?php

header
('HTTP/1.1 301 Moved Permanently', true, 301);

header('foo: bar');
header('a: b');
header('colon less example');

print_r(headers_list());
?>

Array
(
[0] => X-Powered-By: PHP/5.4.7
[1] => foo: bar
[2] => a: b
)
up
19
arnold at jasny dot net
8 years ago
This function won't work for when you're running PHP from the command line. If will always return an empty array. This can be an issue when testing your project using PHPUnit or Codeception.

To solve this, install the xdebug extension and use `xdebug_get_headers` when on the cli.

<?php
$headers
= php_sapi_name() === 'cli' ? xdebug_get_headers() : headers_list();
?>
To Top