PHP headers_sent() function
Definition and Usage
headers_sent() function checks if HTTP headers have been sent and where they were sent.
Returns true if headers have been sent, otherwise returns false.
Syntax
headers_sent(file,line)
Parameter | Description |
---|---|
file,line | Optional. If set file and line Parameters, headers_sent() will store the name of the PHP source file and line number where the output started in the file and line variables. |
Tips and Comments
Comment:Cannot be used once the header block has been sent header() function to send other headers. Using this function can at least avoid error messages related to HTTP headers.
Comment:optional file and line The parameter is a new addition in PHP 4.3.
Example
Example 1
<?php // If headers have not been sent, send one if (!headers_sent()) { header("Location: http://www.codew3c.com/"); exit; } ?> <html> <body> ... ...
Example 2
Use optional file and line parameters:
<?php // Pass $file and $line for future use // Do not pre-assign them if (!headers_sent($file, $line)) { header("Location: http://www.codew3c.com/"); exit; // Trigger an error here } else { echo "Headers sent in $file on line $line"; exit; } ?> <html> <body> ... ...