<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >headers_sent</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Network Functions" HREF="ref.network.html"><LINK REL="PREVIOUS" TITLE="headers_list" HREF="function.headers-list.html"><LINK REL="NEXT" TITLE="inet_ntop" HREF="function.inet-ntop.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.headers-list.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.inet-ntop.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.headers-sent" ></A >headers_sent</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN149437" ></A ><P > (PHP 3 >= 3.0.8, PHP 4, PHP 5)</P >headers_sent -- Checks if or where headers have been sent</DIV ><DIV CLASS="refsect1" ><A NAME="AEN149440" ></A ><H2 >Description</H2 >bool <B CLASS="methodname" >headers_sent</B > ( [string &file [, int &line]] )<BR ></BR ><P > <B CLASS="function" >headers_sent()</B > will return <TT CLASS="constant" ><B >FALSE</B ></TT > if no HTTP headers have already been sent or <TT CLASS="constant" ><B >TRUE</B ></TT > otherwise. If the optional <CODE CLASS="parameter" >file</CODE > and <CODE CLASS="parameter" >line</CODE > parameters are set, <B CLASS="function" >headers_sent()</B > will put the PHP source file name and line number where output started in the <CODE CLASS="parameter" >file</CODE > and <CODE CLASS="parameter" >line</CODE > variables. </P ><P > You can't add any more header lines using the <A HREF="function.header.html" ><B CLASS="function" >header()</B ></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.html" >Output Buffering</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The optional <CODE CLASS="parameter" >file</CODE > and <CODE CLASS="parameter" >line</CODE > parameters were added in PHP 4.3.0. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN149468" ></A ><P ><B >例 1. Examples using <B CLASS="function" >headers_sent()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br /></font><font color="#FF8000">// If no headers are sent, send one<br /></font><font color="#007700">if (!</font><font color="#0000BB">headers_sent</font><font color="#007700">()) {<br /> </font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">'Location: http://www.example.com/'</font><font color="#007700">);<br /> exit;<br />}<br /> <br /></font><font 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 /></font><font color="#007700">if (!</font><font color="#0000BB">headers_sent</font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">, </font><font color="#0000BB">$linenum</font><font color="#007700">)) {<br /> </font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">'Location: http://www.example.com/'</font><font color="#007700">);<br /> exit;<br /><br /></font><font color="#FF8000">// You would most likely trigger an error here.<br /></font><font color="#007700">} else {<br /><br /> echo </font><font color="#DD0000">"Headers already sent in $filename on line $linenum</font><font color="#007700">\n</font><font color="#DD0000">" </font><font color="#007700">.<br /> </font><font color="#DD0000">"Cannot redirect, for now please click this <a " </font><font color="#007700">.<br /> </font><font color="#DD0000">"href=\"http://www.example.com\">link</a> instead\n"</font><font color="#007700">;<br /> exit;<br />}<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.ob-start.html" ><B CLASS="function" >ob_start()</B ></A >, <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A >, and <A HREF="function.header.html" ><B CLASS="function" >header()</B ></A > for a more detailed discussion of the matters involved. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.headers-list.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.inet-ntop.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >headers_list</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.network.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >inet_ntop</TD ></TR ></TABLE ></DIV ></BODY ></HTML >