Source of: /manual/en/function.headers-sent.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.network.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.headers-sent.php',
1 => 'headers_sent',
),
'up' =>
array (
0 => 'ref.network.php',
1 => 'Network Functions',
),
'prev' =>
array (
0 => 'function.headers-list.php',
1 => 'headers_list',
),
'next' =>
array (
0 => 'function.inet-ntop.php',
1 => 'inet_ntop',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.headers-sent" class="refentry">
<div class="refnamediv">
<h1 class="refname">headers_sent</h1>
<p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">headers_sent</span> — <span class="dc-title">Checks if or where headers have been sent</span></p>
</div>
<a name="function.headers-sent.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">bool</span> <span class="methodname"><b>headers_sent</b></span>
([ <span class="methodparam"><span class="type">string</span> <tt class="parameter reference">&$file</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter reference">&$line</tt></span>
]] )</div>
<p class="para rdfs-comment">
Checks if or where headers have been sent.
</p>
<p class="para">
You can't add any more header lines using the <a href="function.header.php" class="function">header()</a>
function once the header block has already been sent. Using this function
you can at least prevent getting HTTP header related error messages.
Another option is to use <a href="ref.outcontrol.php" class="link">Output Buffering</a>.
</p>
</div>
<a name="function.headers-sent.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">file</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
If the optional <i><tt class="parameter">file</tt></i>
and
<i><tt class="parameter">line</tt></i>
parameters are set,
<b>headers_sent()</b> will put the PHP source file name
and line number where output started in the <i><tt class="parameter">file</tt></i>
and <i><tt class="parameter">line</tt></i>
variables.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">line</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The line number where the output started.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.headers-sent.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
<b>headers_sent()</b> will return <b><tt class="constant">FALSE</tt></b> if no HTTP headers
have already been sent or <b><tt class="constant">TRUE</tt></b> otherwise.
</p>
</div>
<a name="function.headers-sent.changelog"></a><div class="refsect1 changelog">
<h3 class="title">Changelog</h3>
<p class="para">
</p><table class="doctable informaltable">
<thead valign="middle">
<tr valign="middle">
<th>Version</th>
<th>Description</th>
</tr>
</thead>
<tbody valign="middle" class="tbody">
<tr valign="middle">
<td align="left">4.3.0</td>
<td align="left">
The optional <i><tt class="parameter">file</tt></i>
and <i><tt class="parameter">line</tt></i>
parameters were added.
</td>
</tr>
</tbody>
</table>
<p>
</p>
</div>
<a name="function.headers-sent.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Examples using <b>headers_sent()</b></b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// If no headers are sent, send one<br /></span><span style="color: #007700">if (!</span><span style="color: #0000BB">headers_sent</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location: http://www.example.com/'</span><span style="color: #007700">);<br /> exit;<br />}<br /><br /></span><span style="color: #FF8000">// An example using the optional file and line parameters, as of PHP 4.3.0<br />// Note that $filename and $linenum are passed in for later use.<br />// Do not assign them values beforehand.<br /></span><span style="color: #007700">if (!</span><span style="color: #0000BB">headers_sent</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #0000BB">$linenum</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location: http://www.example.com/'</span><span style="color: #007700">);<br /> exit;<br /><br /></span><span style="color: #FF8000">// You would most likely trigger an error here.<br /></span><span style="color: #007700">} else {<br /><br /> echo </span><span style="color: #DD0000">"Headers already sent in </span><span style="color: #0000BB">$filename</span><span style="color: #DD0000"> on line </span><span style="color: #0000BB">$linenum</span><span style="color: #DD0000">\n" </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"Cannot redirect, for now please click this <a " </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"href=\"http://www.example.com\">link</a> instead\n"</span><span style="color: #007700">;<br /> exit;<br />}<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="function.headers-sent.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.ob-start.php" class="function" rel="rdfs-seeAlso">ob_start()</a> - Turn on output buffering</li>
<li class="member"><a href="function.trigger-error.php" class="function" rel="rdfs-seeAlso">trigger_error()</a> - Generates a user-level error/warning/notice message</li>
<li class="member"><a href="function.headers-list.php" class="function" rel="rdfs-seeAlso">headers_list()</a> - Returns a list of response headers sent (or ready to send)</li>
<li class="member">
<a href="function.header.php" class="function" rel="rdfs-seeAlso">header()</a> - Send a raw HTTP header for a more detailed discussion of the
matters involved.
</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>