<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.headers-sent.php',
    1 => 'headers_sent',
    2 => '检测消息头是否已经发送',
  ),
  'up' => 
  array (
    0 => 'ref.network.php',
    1 => '网络 函数',
  ),
  'prev' => 
  array (
    0 => 'function.headers-list.php',
    1 => 'headers_list',
  ),
  'next' => 
  array (
    0 => 'function.http-clear-last-response-headers.php',
    1 => 'http_clear_last_response_headers',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/network/functions/headers-sent.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.headers-sent" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">headers_sent</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">headers_sent</span> &mdash; <span class="dc-title">检测消息头是否已经发送</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.headers-sent-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>headers_sent</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter reference">&$filename</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$line</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   检测消息头是否已经发送。
  </p>
  <p class="para">
   消息头已经发送时，就无法通过 <span class="function"><a href="function.header.php" class="function">header()</a></span> 
   添加更多头字段。使用此函数起码可以防止收到跟消息头相关的错误。另一个解决方案是用<a href="ref.outcontrol.php" class="link">输出缓冲</a>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.headers-sent-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       若设置了可选参数 <code class="parameter">filename</code> 和
       <code class="parameter">line</code>，<span class="function"><strong>headers_sent()</strong></span> 会把 PHP 文件名放在
       <code class="parameter">filename</code> 变量里，输出开始的行号放在 <code class="parameter">line</code> 变量里。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        如果在执行 PHP 源文件之前已经开始输出（例如由于启动错误），则 <code class="parameter">filename</code> 参数将被设置为空字符串。
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">line</code></dt>
     <dd>
      <p class="para">
       输出开始的行号。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.headers-sent-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   消息头未发送时，<span class="function"><strong>headers_sent()</strong></span> 返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>，否则返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.headers-sent-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 使用 <span class="function"><strong>headers_sent()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// 没有消息头就发送一个<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">// 使用 file 和 line 参数选项的例子<br />// 注意 $filename 和 $linenum 用于下文中使用<br />// 所以不要提前为它们赋值<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">// 很有可能在这里触发错误<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 &lt;a " </span><span style="color: #007700">.<br />          </span><span style="color: #DD0000">"href=\"http://www.example.com\"&gt;link&lt;/a&gt; instead\n"</span><span style="color: #007700">;<br />    exit;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.headers-sent-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
<p class="para">
 数据头只会在SAPI支持时得到处理和输出。
</p>
</p></blockquote>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.headers-sent-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.ob-start.php" class="function" rel="rdfs-seeAlso">ob_start()</a> - 打开输出控制缓冲</span></li>
    <li><span class="function"><a href="function.trigger-error.php" class="function" rel="rdfs-seeAlso">trigger_error()</a> - 产生一个用户级别的 error/warning/notice 信息</span></li>
    <li><span class="function"><a href="function.headers-list.php" class="function" rel="rdfs-seeAlso">headers_list()</a> - 返回已发送的 HTTP 响应头（或准备发送的）</span></li>
    <li>
     <span class="function"><a href="function.header.php" class="function" rel="rdfs-seeAlso">header()</a> - 发送原生 HTTP 头</span> 中有更多相关细节的讨论。
    </li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>