English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP tutorial

Advanced PHP tutorial

PHP & MySQL

PHP reference manual

PHP headers_sent() function usage and example

PHP HTTP  reference manual

The headers_sent() function checks if the HTTP header has been sent.

Syntax

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

Definition and usage

Check if the HTTP header has been sent.
When the HTTP header has been sent, it is not possible to add more header fields through header(). Using this function at least can prevent HTTP header errors. Another solution is to use output buffering.

Return value

 When the HTTP header has not been sent, headers_sent() returns FALSE, otherwise it returns TRUE.

Parameter

NumberParameters and descriptions
1

file

 If the optional parameters file and line are set, headers_sent() will put the PHP file name in the file variable, and the starting line number in the line variable.

2

line

Output the starting line number.

Online example

Try the following example

<?php
   if (!headers_sent()) {
      header('Location: http://ru.oldtoolbag.com/');
      exit;
   }
   
   if (!headers_sent($filename, $linenum)) {
      header('Location: http://ru.oldtoolbag.com/');
      exit;
   } else {
      echo \
         
         "href = \
      exit;
   }
?>

The following example checks if the header has been sent, if it has been sent, a message is displayed, otherwise the header is sent

PHP HTTP  reference manual