小白教程
所有教程
关于
Search
108.162.216.92
108.162.216.92
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Func http header”的源代码
本页内容
下一节:
Func_http_headers_list
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:header()}}[[Category:PHP HTTP 参考手册|1]] = PHP header() 函数 = [[Category:PHP HTTP 参考手册]] == 定义和用法 == header() 函数向客户端发送原始的 HTTP 报头。 认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数(在 PHP 4 以及更高的版本中,您可以使用输出缓冲来解决这个问题): <sample title="" desc="" lang="php" hererun="1"> <html> <?php // This results in an error. // The output above is before the header() call header('Location: http://www.example.com/'); ?> </sample> == 语法 == <pre> header(string,replace,http_response_code) </pre> {| class="table table-striped table-hover" ! 参数 ! 描述 |- | string | 必需。规定要发送的报头字符串。 |- | replace | 可选。指示该报头是否替换之前的报头,或添加第二个报头。默认是 TRUE(替换)。FALSE(允许相同类型的多个报头)。 |- | http_response_code | 可选。把 HTTP 响应代码强制为指定的值。(PHP 4.3 以及更高版本可用) |} == 提示和注释 == '''注释:'''从 PHP 4.4 之后,该函数防止一次发送多个报头。这是对头部注入攻击的保护措施。 == 实例 1 == 禁用页面缓存: <sample title="" desc="" lang="php" hererun="1"> <?php // Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache"); header("Pragma: no-cache"); ?> <html> <body> ... ... </sample> '''注释:'''用户可能会设置一些选项来更改浏览器的默认缓存设置。通过发送上面的报头,您可以覆盖任何这些设置,强制浏览器不进行缓存! == 实例 2 == 提示用户保存一个生成的 PDF 文件(Content-Disposition 报头用于提供一个推荐的文件名,并强制浏览器显示保存对话框): <sample title="" desc="" lang="php" hererun="1"> <?php header("Content-type:application/pdf"); // It will be called downloaded.pdf header("Content-Disposition:attachment;filename='downloaded.pdf'"); // The PDF source is in original.pdf readfile("original.pdf"); ?> <html> <body> ... ... </sample> '''注释:'''微软 IE 5.5 存在一个阻止以上机制的 bug。通过升级为 Service Pack 2 或更高的版本,可以解决该 bug。 [[Category:PHP HTTP 参考手册]]
返回至“
Func http header
”。
下一节:
Func_http_headers_list