header_register_callback

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

header_register_callbackLlamar a una función de cabecera

Descripción

header_register_callback(callable $callback): bool

Registra una función que será llamada cuando PHP comienza a enviar la salida.

El callback se ejecuta inmediatamente después de PHP prepara todos los encabezados que van a ser enviados, y antes de cualquier otra salida es enviado, crea una ventana para manipular las cabeceras de salida antes de ser enviado.

Parámetros

callback

Función llamada justo antes de que se envíen los encabezados. No tiene parámetros y el valor de retorno se ignora.

Valores devueltos

Esta función retorna true en caso de éxito o false si ocurre un error.

Ejemplos

Ejemplo #1 header_register_callback() example

<?php

header
('Content-Type: text/plain');
header('X-Test: foo');

function
foo() {
foreach (
headers_list() as $header) {
if (
strpos($header, 'X-Powered-By:') !== false) {
header_remove('X-Powered-By');
}
header_remove('X-Test');
}
}

$result = header_register_callback('foo');
echo
"a";
?>

Resultado del ejemplo anterior es similar a :

Content-Type: text/plain

a

Notas

La función header_register_callback() es ejecutada cuando las cabeceras están a punto de ser enviadas, por lo que cualquier salida de esta función puede romper de salida.

Nota:

Los encabezados solo serán accesibles y se mostrarán cuando se utilice un SAPI que los soporte.

Ver también

add a note

User Contributed Notes 1 note

up
13
matt@kafene
12 years ago
Note that this function only registers a single callback as of php 5.4. The most recent callback set is the one that will be executed, they will not be executed in order like with register_shutdown_function(), just overwritten.

Here is my test:

<?php

$i
= $j = 0;
header_register_callback(function() use(&$i){ $i+=2; });
header_register_callback(function() use(&$i){ $i+=3; });
register_shutdown_function(function() use(&$j){ $j+=2; });
register_shutdown_function(function() use(&$j){ $j+=3; });
register_shutdown_function(function() use(&$j){ var_dump($j); });
while(!
headers_sent()) { echo "<!-- ... flushing ... -->"; }
var_dump(headers_sent(), $i);
exit;

?>

Results:

headers_sent() - true
$i = 3
$j = 5
To Top