Home  >  Article  >  php教程  >  php header()函数详细介绍与实例

php header()函数详细介绍与实例

WBOY
WBOYOriginal
2016-06-13 10:00:19753browse

php教程 header()函数详细介绍与实例
语法
header(string,replace,http_response_code)参数 描述
string 必需。规定要发送的报头字符串。
replace 可选。指示该报头是否替换之前的报头,或添加第二个报头。

默认是 true(替换)。false(允许相同类型的多个报头)。
 
http_response_code 可选。把 http 响应代码强制为指定的值。(php 4 以及更高版本可用)


header() 函数向客户端发送原始的 http 报头。

认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 php 4 以及更高的版本中,您可以使用输出缓存来解决此问题):


*/
header("x-sample-test:foo");       //发送http标头
header('content-type:text/plain');      //发送http标头
var_dump(headers_list());        //返回已发送的标头列表

if(!headers_sent())          //如果标头没有发送
{
  header('location:http://www.example.com/');     //发送标头
  exit;            //结束php代码
}
if(!headers_sent($filename,$linenum))      //如果没有输出指定文件
{
  header('location:http://www.example.com/');     //发送标头
  exit;            //结束php代码
}
else             //如果已经输出到指定文件
{
  echo "headers already sent in $filename on line $linenumn".
  "cannot redirect,for now please click this   "href="http://www.example.com">linkinsteadn";   //输出提示信息
  exit;            //结束php代码
}
/*
注释:从 php 4.4 之后,该函数防止一次发送多个报头。这是对头部注入攻击的保护措施。

*/

Statement:
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