search for in the  
<headers_listsetcookie>
Last updated: Thu, 19 May 2005

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.

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

Example 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.



User Contributed Notes
headers_sent
K.Tomono
22-Apr-2005 12:24
[code]
<?php
header
("Cache-Control: private, must-revalidate, max-age=3600, post-check=3600, pre-check=3600");
////header("Last-Modified: " . gmdate("D, d M Y H:i:s",getlastmod())." GMT");
////ini_set("last_modified","1");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
flush(); // <= (*1)
...
if (!
headers_sent()) {
 
header('Content-Type:text/html; charset='._CHARSET);
 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
 
//header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
 
header('Cache-Control: private, no-cache');
 
header('Pragma: no-cache');
}
...
?>
[/code]

headers_sent() does not evaluate it as true, unless the flush()(*1) has been done.

It seems that it does not mean header was sent unless a header output is taken
out to the exterior of PHP.

Apache 2.0.53 (prefork)
PHP 5.0.3 (server module)
... And XOOPS 2.0.9.2

I had seldom paid attention to flush() on PHP which is not C.
However, it might have been a required thing.

[pre]
$ curl --cookie PHPSESSID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -i \
"http ://myhost.mydomain/xoops/modules/test.php?i=1" | less
  % Total    % Received % Xferd  Average Speed          Time            Curr.
                                 Dload  Upload Total    Current  Left    Speed
  0    0    0    0    0    0      0      0 --:--:--  0:00:00 --:--:--    0

HTTP/1.1 200 OK
Date: Fri, 22 Apr 2005 05:00:11 GMT
Server: Apache
X-Powered-By: PHP/5.0.3
Set-Cookie: PHPSESSID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, must-revalidate, max-age=3600, post-check=3600, pre-check=3600
Pragma: no-cache
Last-Modified: Fri, 22 Apr 2005 05:00:11 GMT
Transfer-Encoding: chunked
Content-Type: text/html
[/pre]
(*)"http :" is "http:" in fact.
Terry
10-Feb-2005 09:58
For programmers used to Perl, note that sending a relative 'Location:' header sends a redirect to the browser in PHP, unlike Perl which will attempt to call relative URLs using an internal subrequest and return that page to the browser without redirecting.  If you want to do the same trick in PHP, use include() or virtual().
php at fufachew dot REMOVEME dot com
28-Feb-2004 01:26
RE: antti at haapakangas dot net's post

I've changed the code so $_SERVER['SERVER_NAME'] is used if $_SERVER['HTTP_HOST'] is not set.  $_SERVER['SERVER_NAME'] doesn't meet my needs, but I suppose it's good to fall back on it.  I've also fixed a problem in the meta refresh line - it was missing the "url=" part of the content attribute.

<?php
function server_url()

  
$proto = "http" .
       ((isset(
$_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "s" : "") . "://";
  
$server = isset($_SERVER['HTTP_HOST']) ?
      
$_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
   return
$proto . $server;
}
  
function
redirect_rel($relative_url)
{
  
$url = server_url() . dirname($_SERVER['PHP_SELF']) . "/" . $relative_url;
   if (!
headers_sent())
   {
      
header("Location: $url");
   }
   else
   {
       echo
"<meta http-equiv=\"refresh\" content=\"0;url=$url\">\r\n";
   }
}
?>
antti at haapakangas dot net
28-Jan-2004 02: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: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT