mirror of
https://github.com/php/doc-zh.git
synced 2026-04-27 10:16:00 +02:00
144 lines
4.0 KiB
XML
144 lines
4.0 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: b4989f5c1f052314ff2c36e9cc3d56b3a7a24e9b Maintainer: daijie Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi -->
|
||
<refentry xml:id="function.headers-sent" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>headers_sent</refname>
|
||
<refpurpose>检测消息头是否已经发送</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>bool</type><methodname>headers_sent</methodname>
|
||
<methodparam choice="opt"><type>string</type><parameter role="reference">filename</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter role="reference">line</parameter><initializer>&null;</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
检测消息头是否已经发送。
|
||
</para>
|
||
<para>
|
||
消息头已经发送时,就无法通过 <function>header</function>
|
||
添加更多头字段。使用此函数起码可以防止收到跟消息头相关的错误。另一个解决方案是用<link
|
||
linkend="ref.outcontrol">输出缓冲</link>。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>filename</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
若设置了可选参数 <parameter>filename</parameter> 和
|
||
<parameter>line</parameter>,<function>headers_sent</function> 会把 PHP 文件名放在
|
||
<parameter>filename</parameter> 变量里,输出开始的行号放在 <parameter>line</parameter> 变量里。
|
||
</para>
|
||
<note>
|
||
<para>
|
||
如果在执行 PHP 源文件之前已经开始输出(例如由于启动错误),则 <parameter>filename</parameter> 参数将被设置为空字符串。
|
||
</para>
|
||
</note>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>line</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
输出开始的行号。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
消息头未发送时,<function>headers_sent</function> 返回 &false;,否则返回 &true;。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title>使用 <function>headers_sent</function> 的例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
// 没有消息头就发送一个
|
||
if (!headers_sent()) {
|
||
header('Location: http://www.example.com/');
|
||
exit;
|
||
}
|
||
|
||
// 使用 file 和 line 参数选项的例子
|
||
// 注意 $filename 和 $linenum 用于下文中使用
|
||
// 所以不要提前为它们赋值
|
||
if (!headers_sent($filename, $linenum)) {
|
||
header('Location: http://www.example.com/');
|
||
exit;
|
||
|
||
// 很有可能在这里触发错误
|
||
} else {
|
||
|
||
echo "Headers already sent in $filename on line $linenum\n" .
|
||
"Cannot redirect, for now please click this <a " .
|
||
"href=\"http://www.example.com\">link</a> instead\n";
|
||
exit;
|
||
}
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="notes">
|
||
&reftitle.notes;
|
||
¬e.network.header.sapi;
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>ob_start</function></member>
|
||
<member><function>trigger_error</function></member>
|
||
<member><function>headers_list</function></member>
|
||
<member>
|
||
<function>header</function> 中有更多相关细节的讨论。
|
||
</member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
</refentry>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|