php tutorial header() function detailed introduction and examples
Grammar
header(string,replace,http_response_code) parameter description
string required. Specifies the header string to be sent.
replace optional. Indicates whether this header replaces the previous header, or adds a second header.
Default is true (replacement). false (allow multiple headers of the same type).
http_response_code Optional. Force the http response code to the specified value. (Available in php 4 and above)
The header() function sends raw http headers to the client.
It is important to realize that the header() function must be called before any actual output is sent (in PHP 4 and above, you can use output caching to solve this problem):
*/
header("x-sample-test:foo"); //Send http header
header('content-type:text/plain'); //Send http header
var_dump(headers_list()); //Return the sent header list
if(!headers_sent()) //If headers are not sent
{
header('location:http://www.example.com/'); //Send header
exit; //End php code
}
if(!headers_sent($filename,$linenum)) //If the specified file is not output
{
header('location:http://www.example.com/'); //Send header
exit; //End php code
}
else //If it has been output to the specified file
{
echo "headers already sent in $filename on line $linenumn".
"cannot redirect,for now please click this
"href="http://www.example.com">linkinsteadn"; //Output prompt message
exit; //End php code
}
/*
Note: Since PHP 4.4, this function prevents multiple headers from being sent at once. This is a protection measure against header injection attacks.
*/