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

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method