Detailed introduction and examples of php header() function_PHP tutorial

WBOY
Release: 2016-07-13 16:55:14
Original
875 people have browsed it

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.

*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631712.htmlTechArticlephp tutorial header() function detailed introduction and example syntax header(string,replace,http_response_code) parameter description string required. Specifies the header string to be sent. replace optional. Instructions...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!