headers_sent
(PHP 4, PHP 5, PHP 7)
headers_sent — Checks if or where headers have been sent
Description
&$file
[, int &$line
]] ) : boolChecks 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.
Parameters
-
file
-
If the optional
file
andline
parameters are set, headers_sent() will put the PHP source file name and line number where output started in thefile
andline
variables. -
line
-
The line number where the output started.
Return Values
headers_sent() will return FALSE
if no HTTP headers
have already been sent or TRUE
otherwise.
Examples
Example #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;
}
?>
See Also
- ob_start() - Turn on output buffering
- trigger_error() - Generates a user-level error/warning/notice message
- headers_list() - Returns a list of response headers sent (or ready to send)
- header() - Send a raw HTTP header for a more detailed discussion of the matters involved.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.headers-sent.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.