PHP headers_sent() Function

❮ PHP Network Reference

Example

If no headers are sent, send one:

<?php
if (!headers_sent()) {
  header("Location: https://www.w3schools.com/");
  exit;
}
?>

<html>
<body>

...
...


Definition and Usage

The headers_sent() function checks if/where headers have been sent.

Syntax

headers_sent(file,line)

Parameter Values

Parameter Description
file Optional. If the file and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the file and line variables
line Optional. Specifies the line number where the output started


Technical Details

Return Value: TRUE if HTTP headers has been sent, FALSE otherwise
PHP Version: 4.0+
PHP Changelog: PHP 4.3: Added the optional file and line parameters

More Examples

Example

Using the optional file and line parameters:

<?php
// $file and $line are passed in for later use
// Do not assign them values beforehand
if (!headers_sent($file, $line))
  {
  header("Location: https://www.w3schools.com/");
  exit;
  // Trigger an error here
  }
else
  {
  echo "Headers sent in $file on line $line";
  exit;
  }
?>

<html>
<body>

...
...

❮ PHP Network Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.