超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<headers_listsetcookie>
Last updated: Fri, 22 Jun 2007

headers_sent

(PHP 3>= 3.0.8, PHP 4 , PHP 5)

headers_sent -- Checks if or where headers have been sent

Description

bool headers_sent ( [string &file [, int &line]])

headers_sent() will return FALSE if no HTTP headers have already been sent or TRUE otherwise. If the optional file and line parameters are set, headers_sent() will put the PHP source file name and line number where output started in the file and line variables.

You can't add any more header lines using the header() 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 Output Buffering.

注: The optional file and line parameters where added in PHP 4.3.0.

例子 1. Examples using headers_sent()

<?php

// If no headers are sent, send one
if (!headers_sent()) {
  
header('Location: http://www.example.com/');
   exit;
}
 
// An example using the optional file and line parameters, as of PHP 4.3.0
// Note that $filename and $linenum are passed in for later use.
// Do not assign them values beforehand.
if (!headers_sent($filename, $linenum)) {
  
header('Location: http://www.example.com/');
   exit;

// You would most likely trigger an error here.
} 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;
}

?>

See also ob_start(), trigger_error(), and header() for a more detailed discussion of the matters involved.




add a note add a note User Contributed Notes
headers_sent
antti at haapakangas dot net
29-Jan-2004 04:39
Re: php at fufachew dot com

That's a nice example how to implement Location header in a correct way (using absoluteURI). 95% of the scripts I have seen just use relativeURI which is wrong. Some browsers, for example lynx, actually notify user about incomplete Location headers. However it might be safer to use $_SERVER['SERVER_NAME'] instead of $_SERVER['HTTP_HOST']. Host header is a HTTP/1.1 feature and you can not count on that if you want to be interoperable with HTTP/1.0 implementations.

<headers_listsetcookie>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22