PHP headers_sent() function

Definition and Usage

headers_sent() function checks whether the HTTP headers have been sent and where they have been sent.

If headers have been sent, it returns true, otherwise it returns false.

Syntax

headers_sent(file,line)
Parameter Description
file,line

Optional.

If set file and line The parameter, headers_sent() will store the name of the PHP source file and line number where the output starts in the file and line variables.

Tips and Comments

Comment:Once the header block has been sent, it cannot be used header() 함수 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.

Instance

예제 1

<?php
// If headers have not been sent, send one
if (!headers_sent())
  {
  header("Location: http://www.codew3c.com/");
  exit;
  }
?>
<html>
<body>
...
...

예제 2

Optional file and line parameters can be used:

<?php
// Pass $file and $line for future use
// Do not assign values to them in advance
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>
...
...