一聚教程网:一个值得你收藏的教程网站

热门教程

PHP header 函数教程

时间:2022-07-02 10:00:00 编辑:袖梨 来源:一聚教程网

PHP header 函数教程

定义和用法
header ( )函数发送一个原始HTTP头到客户端。

重要的是要看到,标题( )必须在任何所谓的实际产出发送(在PHP 4和以后,您可以使用输出缓冲来解决这个问题) :

// This results in an error.
// The output above is before the header() call
header('Location: http://www.example.com/');
?>

语法:

header(string,replace,http_response_code)
ParameterDescription
string
必需的。指定的标题字符串发送
replace
任择。指示是否标题应取代以前或添加第二个标题。预设值是true (将取代) 。假(允许多个标题同一类型)
http_response_code
任择。部队的HTTP响应代码到指定的值(可在PHP 4.3和更高)

提示和说明
注:自PHP 4.4这一功能可以防止一个以上的标题发送一次。这是一个保护,防止头注入攻击。




范例1
防止页面缓存:

 

// Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache"); header("Pragma: no-cache");

 

示例2
让用户将提示保存生成的PDF文件(内容处置标题是用来提供建议的文件名,并迫使浏览器来显示保存对话框) :

 

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");

热门栏目