Change language

PHP headers_sent () function

|
Syntax:
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.
The headers_sent() function puts the PHP source file name and line number from which output starts in file and line variables if file and line options are given. Return Value:This function returns True if headers were sent, and false otherwise.Example 1:  
// 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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically