suche nach in der

http_response_code> <headers_list
Last updated: Fri, 18 May 2012

view this page in

headers_sent

(PHP 4, PHP 5)

headers_sentPrüft, ob oder wo die Header bereits gesendet wurden

Beschreibung

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

Prüft, ob oder wo die Header bereits gesendet wurden.

Sobald der Header-Block gesendet wurde, können Sie keine weiteren Header-Zeilen mit der Funktion header() hinzufügen. Mittels dieser Funktion können Sie aber zumindest Fehlermeldungen bezüglich der HTTP-Header vermeiden. Eine andere Möglichkeit ist die Verwendung von Output Buffering.

Parameter-Liste

file

Sind die optionalen Parameter file und line angegeben, schreibt headers_sent() den PHP-Dateinamen und die Zeilennummer, an denen die Ausgabe begann, in die Variablen file und line.

line

Die Zeilennummer, an der die Ausgabe begann.

Rückgabewerte

headers_sent() gibt FALSE zurück, wenn noch keine HTTP-Header gesendet wurden, ansonsten TRUE.

Changelog

Version Beschreibung
4.3.0 Die optionalen Parameter file und line wurden eingeführt.

Beispiele

Beispiel #1 Beispiel zur Verwendung von headers_sent()

<?php

// Wenn keine Header gesendet wurden, sende einen
if (!headers_sent()) {
    
header ('Location: http://www.example.com/');
    exit;
}

// Ein Beispiel für den Einsatz der seit PHP 4.3.0 verfügbaren optionalen
// Parameter file und line. Beachten Sie, dass $filename und $linenum für
// einen späteren Einsatz übergeben werden.
// Weisen Sie ihnen davor keine Werte zu.
if (!headers_sent($filename$linenum)) {
    
header ('Location: http://www.example.com/');
    exit;

// Wahrscheinlich wollen Sie hier eine Fehlermeldung auslösen
} else {

    echo 
"Header bereits gesendet in $filename in Zeile $linenum\n" .
         
"Redirect nicht moeglich, klicken Sie daher statt dessen <a " .
         
"href=\"http://www.example.com\">diesen Link</a> an\n";
    exit;
}

?>

Anmerkungen

Hinweis:

Header sind nur dann zugänglich und werden nur dann gesendet, wenn die genutzte SAPI sie unterstützt.

Siehe auch

  • ob_start() - Ausgabepufferung aktivieren
  • trigger_error() - Erzeugt eine benutzerdefinierte Fehlermeldung/Warnung/Benachrichtigung
  • headers_list() - Gibt eine Liste der gesendeten (oder zum Senden vorbereiteten) Response Header zurück
  • header() - Sendet einen HTTP-Header in Rohform für eine detailliertere Darlegung der relevanten Konzepte.



add a note add a note User Contributed Notes
headers_sent
collectours at free dot fr
18-Jun-2007 11:32
In response to K.Tomono and alexrussell101 at gmail dot com :

Yes,
headers_sent() will return false, even if you sent something to the ouptut using print() or header() , if output_buffering is different from Off in you php.ini, and the length of what you sent does not exceed the size of output_buffering.

To test it, try this code with  these values in php.ini
1) output_buffering=32
2) output buffering = 4096

[code]
<?php
   
echo "Yo<br />";
    echo
"Sent:",headers_sent(),"<br />";
    echo
"enough text to feed the buffer until it overflows ;-)<br />";
    echo
"Sent:",headers_sent(),"<br />";
?>
[/code]

then put
3) output buffering = Off
and try this code
[code]
<?php
   
echo "Yo<br />";
    echo
"Sent:",headers_sent(),"<br />";
?>
[/code]
which will this time unconditionnally say that headers were sent.

This is noticed in php.ini comment :
"Output buffering allows you to send header lines (including cookies) even after you send body content, in the price of slowing PHP's output layer a bit."

Note : This is completly independant of implicit_flush tuning.
kamermans at teratechnologies dot net
21-Aug-2006 10:24
If you are using output buffering and you use the flush() command ANYWHERE headers_sent() will return true - even if the buffer is seemingly empty.
rajnesh at gmail dot com
25-Feb-2006 07:19
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.
Jakob B.
07-Jan-2006 12:03
<?php
function redirect($filename) {
    if (!
headers_sent())
       
header('Location: '.$filename);
    else {
        echo
'<script type="text/javascript">';
        echo
'window.location.href="'.$filename.'";';
        echo
'</script>';
        echo
'<noscript>';
        echo
'<meta http-equiv="refresh" content="0;url='.$filename.'" />';
        echo
'</noscript>';
    }
}
redirect('http://www.google.com');
?>
trevize (shtrudel) gmail.com
30-Nov-2005 03:43
Note that in IIS (or at least the version that comes with W2K server), the server seems to do some buffering, so even if you output someting or cause a warning, the value of headers_sent() may be false because the headers haven't been sent yet.

So it's not a safe way to know if warnings have been encountered in your script.
php [at] barryhunter [.] co [.] uk
09-Sep-2005 12:42
In responce to: Terry 11-Feb-2005 03:58

if PHP is run as a Module, then it will behave as you describe

However if PHP is run as a CGI then it will be behave like Perl, (which uses CGI unless its mod_perl), as this is a CGI behaviour.

See http://ppewww.ph.gla.ac.uk/~flavell/www/perlcgifaq.html
alexrussell101 at gmail dot com
02-Sep-2005 12:31
To K.Tomono:

Headers are not sent as soon as you call the header() function. They are only sent as soon as some body content (i.e. HTML via echo or escaping from PHP parsing mode) is reached (or, like you did, you send a flush.) Thus after calling header a few times at the beginning they are still unsent and when you call headers_sent() it knows they haven't been sent and reports so. Only when the script ends or you output from content do all the headers so far send.
I think you misunderstood the way they are done and hopefully this should clear a few things up for you.
mark at dreamjunky dot com
28-Jun-2005 06:56
In case this comes up with anyone else, you might trigger headers to be sent if you have a PHP file with extra space after the closing ?>.  In particular, if you include that file at the top of your main script, it will cause headers to send, followed by the space after the ?> in your included script.  In short, make sure you don't have any space trailing your final ?>.
K.Tomono
22-Apr-2005 07: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
11-Feb-2005 04: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 08: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 09: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.

http_response_code> <headers_list
Last updated: Fri, 18 May 2012