bool headers_sent ($file, $line)Parameters: this function takes two parameters as above and described below:
- $file:this parameter contains the file name. This is an optional parameter.
- $line:this parameter contains the line number from which the output began. It is also an optional parameter.
// PHP program to illustrate the headers_sent() function
if
(! headers_sent()) {
header (
"Location: https://en.wikipedia.org/wiki/Main_Page "
);
exit
();
}
?>
Output: In this example, the header has not been sent before, so! Headers_sent() will be TRUE followed by the header ("Location: https://en.wikipedia.org/wiki/Main_Page"); will send the header information and according to the header it will be redirected to the site that is in the parameter.Note:The header cannot be added after the header block has been sent. So this feature is implemented to avoid errors like.Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd3/798/438108/public_html/gfg/header.php : 1) in /storage/ssd3/798/438108/public_html/gfg/header.php on line 4
Example 2: header (
"Expires: Sun, 25 Jul 1997 06:02:34 GMT "
);
?>
hello world < / p>
// header is sent after header
// sent and after the start of the output.
header (
"Cache-Control: no-cache"
);
header (
"Pragma: no -cache "
);
?>
< / html >
Note.Example given here won’t work in the online IDE as it doesn’t support redirecting and changing headers. So, try running it on the host server or localhost.
Exit :
hello world
Warning :Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd3/798/438108/public_html/gfg/header.php:4) in /storage/ssd3/798/438108/public_html/gfg/header.php on line 9
Warning: Unable to change header information - headers already sent
(output started at /storage/ssd3/798/438108/public_html/gfg/header.php:4)
in / storage / ssd3 / 798/438108 / public_html / gfg / header. php on line 10 This error can be resolved using the headers_sent() function.
Example 3:After fixing the above error. header (
"Expires: Sun, 25 Jul 1997 06:02:34 GMT"
);
?>
hello world < / p>
// if the header hasn’t been sent yet, a new header will be sent
if
(! headers_sent()) {
header (
"Cache-Control: no-cache"
);
header (
"Pragma: no-cache"
);
}
?>
< / html >
OUTPUT:
hello world
Explaination:
The headers_sent() function checks if the header has already been sent or not. So! headers_sent() returns false, for which the header is not sent again, avoiding errors.
NOTE.The example shown here will not work in the online IDE as it does not support redirecting and changing headers. So, try it on some hosting server or localhost.Example 4:
// Function to extract the relative URL to create
// full URL with the specified relative URL
function
server_url() {
$proto
=
"http"
.
((isset (
$_ SERVER
[
’HTTPS’
]) & amp; & amp;
$_ SERVER
[
’HTTPS’
] ==
" on "
)?
"s"
:
""
).
": //"
;
$server
= isset (
$_ SERVER
[
’HTTP_HOST’
])?
$_ SERVER
[ ’HTTP_HOST’
]:
$_ SERVER
[
’ SERVER_NAME’
];
return
$proto
.
$server
;
}
// Relative URL redirection function
function
redirect_rel (
$relative_url
) {
$url
= server_url(). dirname (
$_ SERVER
[
’PHP_SELF’
])
.
"/"
.
$relative_url
;
// Check if the title was sent or not
if
(! headers_sent()) {
// If the title is not sent, then the title is sent
header (
"Location: $url"
);
}
else
{
// If the title is sent
echo
"< meta http-equiv =" refresh "content =" 0; url = $url ">"
;
}
}
redirect_rel (
" server.php "
);
?>
Output: Link: http://php.net/manual/en/function.headers-sent.php