PHP headers_sent()Function


PHP HTTP 参考手册Complete PHP HTTP Reference Manual

Definition and Usage

The headers_sent() function checks whether HTTP headers have been sent/where they have been sent.

If the headers have been sent, this function returns TRUE; otherwise, it returns FALSE.

Syntax

headers_sent(file,line)

Parameters Description
file,line Optional. If the file and line parameters are set, headers_sent() will store the PHP source filename and line number where the output started in the file and line variables.


Tips and Notes

Note:Once the header block has been sent, you cannot use the header() function to send other headers.

Note:The optional file and line parameters are new in PHP 4.3.


Example 1

<?php
// If no headers are sent, send one
if (!headers_sent())
{
header("Location: http://www.w3cschool.cc/");
exit;
}
?>

<html>
<body>

...
...


Example 2

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: http://www.w3cschool.cc/");
exit;
// Trigger an error here
}
else
{
echo "Headers sent in $file on line $line";
exit;
}
?>

<html>
<body>

...
...


PHP HTTP 参考手册Complete PHP HTTP Reference Manual Other Extensions