headers_sent

(PHP 4, PHP 5, PHP 7, PHP 8)

headers_sentChecks if or where headers have been sent

Descrição

headers_sent(string&$filename = null, int&$line = null): bool

Checks if or where headers have been sent.

You can't add any more header lines using the header() function once the header block has already been sent. Using this function you can at least prevent getting HTTP header related error messages. Another option is to use Output Buffering.

Parâmetros

filename

If the optional filename and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the filename and line variables.

Nota:

If the output has started before executing the PHP source file (for example due to a startup error), the filename parameter will be set to an empty string.

line

The line number where the output started.

Valor Retornado

headers_sent() will return false if no HTTP headers have already been sent or true otherwise.

Exemplos

Exemplo #1 Examples using headers_sent()

<?php

// If no headers are sent, send one
if (!headers_sent()) {
header('Location: http://www.example.com/');
exit;
}

// An example using the optional file and line parameters
// Note that $filename and $linenum are passed in for later use.
// Do not assign them values beforehand.
if (!headers_sent($filename, $linenum)) {
header('Location: http://www.example.com/');
exit;

// You would most likely trigger an error here.
} else {

echo
"Headers already sent in $filename on line $linenum\n" .
"Cannot redirect, for now please click this <a " .
"href=\"http://www.example.com\">link</a> instead\n";
exit;
}

?>

Notas

Nota:

Os cabeçalhos só serão acessíveis e enviados quando uma SAPI que os suporta estiver em uso.

Veja Também

  • ob_start() - Ativa o buffer de saída
  • trigger_error() - Gera uma mensagem a nível de usuário de erro/alerta/nota
  • headers_list() - Retorna uma lista de cabeçalhos de resposta enviados (ou prontos para enviar)
  • header() - Send a raw HTTP header for a more detailed discussion of the matters involved.
To Top