headers_sent Checks if or where headers have been sent php函数


(PHP 4, PHP 5)

headers_sentChecks if or where headers have been sent


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

Checks if or where headers have been sent.

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.



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.


The line number where the output started.


headers_sent() will return FALSE if no HTTP headers have already been sent or TRUE otherwise.


版本 说明
4.3.0 The optional file and line parameters were added.


Example #1 Examples using headers_sent()


// If no headers are sent, send one
if (!headers_sent()) {
header('Location: http://www.example.com/');

// 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/');

// You would most likely trigger an error here.
} else {

"Headers already sent in $filename on line $linenum.
"Cannot redirect, for now please click this <a " .
"href="http://www.example.com">link</a> instead ";




Headers will only be accessible and output when a SAPI that supports them is in use.


  • ob_start() - 打开输出控制缓冲
  • trigger_error() - Generates a user-level error/warning/notice message
  • headers_list() - Returns a list of response headers sent (or ready to send)
  • header() - Send a raw HTTP header for a more detailed discussion of the matters involved.

header_register_callback Call a header function
header_remove Remove previously set headers
hebrev 将逻辑顺序希伯来文(logical-Hebrew)转换为视觉顺序希伯来文(visual-Hebrew)
hebrevc 将逻辑顺序希伯来文(logical-Hebrew)转换为视觉顺序希伯来文(visual-Hebrew),并且转换换行符
hex2bin Converts the hex representation of data to binary
hexdec 十六进制转换为十进制
highlight_file Syntax highlighting of a file
highlight_string Syntax highlighting of a string
htmlentities Convert all applicable characters to HTML entities
htmlspecialchars Convert special characters to HTML entities
htmlspecialchars_decode Convert special HTML entities back to characters
html_entity_decode Convert all HTML entities to their applicable characters
http_build_query 生成 URL-encode 之后的请求字符串
http_response_code Get or Set the HTTP response code

Copyright © 2016 phpStudy |