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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP headers_sent() function usage and example

PHP HTTP Reference Manual

The headers_sent() function detects whether the HTTP header has been sent.

Syntax

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

Definition and Usage

Detect 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 prevents 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 Description
1

file

 If optional parameters file and line are set, headers_sent() will put the PHP file name in the file variable, and output 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: \//www.oldtoolbag.com/');
      exit;
   }
   
   
      header('Location: \//www.oldtoolbag.com/');
      exit;
   } else {
      echo \
         
         "href = \//www.oldtoolbag.com\">link</a> instead
";
      exit;
   }
?>

The above 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