教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php header()函数详细介绍与实例

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

发布时间:2016-12-04   编辑:jiaochengji.com
教程集为您提供php header()函数详细介绍与实例等资源,欢迎您收藏本站,我们将为您提供最新的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 <a".
  "href="http://www.example.com">link</a>insteadn";   //输出提示信息
  exit;            //结束php代码
}
/*
注释:从 php 4.4 之后,该函数防止一次发送多个报头。这是对头部注入攻击的保护措施。

*/

您可能感兴趣的文章:
php header头信息应用举例
php导出word文档与excel表格文件
php header()函数的简单例子
php header 使用详解
php中header函数的用法举例详解
php生成csv文件header设置参考
php header函数用法详解举例(2)
php readfile函数下载文件并判断权限的代码示例
php header()函数详细介绍与实例
php应用开发之文件下载详解

[关闭]
~ ~