`

php header() 函数使用方法详解

    博客分类:
  • php
阅读更多
Definition and Usage
定义和用法

The header() function sends a raw HTTP header to a client.
header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到客户端。

It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem):
我们必须谨记,header()必须在任何结果被发送之前请求(在PHP4 及以上版本里,你可以使用输出缓冲[output buffer]来解决这个问题)

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

Syntax
语法

header(string,replace,http_response_code)

Parameter参数 Description描述
string Required. Specifies the header string to send
必要参数。指定需要发送的header字符串
replace Optional. Indicates whether the header should replace previous or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type)
可选参数。指明是否需要替代先前的header或重新添加第二个header
http_response_code Optional. Forces the HTTP response code to the specified value (available in PHP 4.3 and higher)
可选参数。使HTTP返回指定值的代码(仅在PHP4.3以上版本中支持)
Tips and Notes
注意点

Note: Since PHP 4.4 this function prevents more than one header to be sent at once. This is a protection against header injection attacks.
注意:从PHP4.4版本开始,这个函数将不允许一次发送多个header[标头]。这将保护header免受攻击。
Example 1
案例1

Prevent page caching:
禁用页面缓存

<?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>

......

Note: There are options that users may set to change the browser's default caching settings. By sending the headers above, you should override any of those settings and force the browser to not cache!
注意:这里列出了多个header供用户选择,由此,用户可以改变浏览器默认的缓设置;通过发送上述的header,你可以忽略所有的设置,并强制禁用浏览器的缓存设置。
Example 2
案例2

Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box):
让用户快速保存一个产生的PDF文件(Content-Disposition header的作用是:提供一个推荐使用的文件名并使浏览器显示“保存对话框[save dialog box]”),具体如下:

<?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.pdfreadfile("original.pdf");?>

<html><body>

......

Note: There is a bug in Microsoft IE 5.5 that prevents this from working. The bug can be resolved by upgrading to Service Pack 2 or later.
注意:在MS IE 5.5中存在一个bug,他会阻止上述代码的运行;你可以升级到Service Pack 2或更高版本来解决这个问题。
-
分享到:
评论

相关推荐

    PHP header函数分析详解

    在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题。下面是header的一些详细讲解。希望对phper有帮助 复制代码 代码如下: &lt;?php // fix 404 pages: header(...

    PHP中header函数的用法及其留意事项详解_.docx

    PHP中header函数的用法及其留意事项详解_.docx

    PHP中header函数的用法及其注意事项详解

    1、使用header函数进行跳转页面;  header(‘Location:’.$url);  其中$url就是将要跳转的url了。  这种用法的注意事项有以下几点: •Location和”:”之间不能有空格,否则会出现错误(注释:我刚测试了,在我...

    header函数设置响应头解决php跨域问题实例详解

    header("Access-Control-Allow-Origin:*"); 2、允许指定域名访问 header( 'Access-Control-Allow-Origin:http://a.test.com' ); 设置允许访问的请求方式: 1、一种或者多种 header('Access-Control-Allow-Methods:...

    Php header()函数语法及使用代码

    Php header()函数语法及使用代码详解,这里列出了header的常用常用状态码。

    详解WordPress开发中get_header()获取头部函数的用法

    get_header函数声明(定义) 之前写文章很少会写到函数定义的代码,后来自己翻看的时候发现这个习惯不太好,所以决定,只要篇幅允许,就会把函数主题贴出来,方便自己翻看。 get_header 函数,声明(定义)的位置,...

    Linux php出现中文乱码的解决方法.docx

    另一种方法是使用 PHP 的 `header` 函数来设置字符编码。在我们的 PHP 代码中添加以下代码: `header('Content-Type: text/html; charset=gb2312');` 这样,我们的 PHP 代码就将使用 GB2312 编码方式,这样可以...

    PHP判断是否为空的几个函数对比

    format的用法(详解)PHP常用函数总结(180多个)PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): …PHP中鲜为人知的10个函数php用header函数实现301跳转代码实例PHP header()函数...

    PHP跳转页面的几种实现方法详解

    •PHP页面跳转一、header()函数header()函数是PHP中进行页面跳转的一种十分简单的方法。header()函数的主要功能是将HTTP协议标头(header)输出到浏览器。... header函数中Location类型的标头是一种特殊的header调用,

    详解WordPress中调用评论模板和循环输出评论的PHP函数

    comments_template 函数是一个调用评论模板的函数,使用起来很简单,与get_header()等函数一样,是一个include文件类函数,今天来讲一下他的使用。 描述 上面已经讲过了,就是调用评论模板的一个函数。 使用 &lt;...

    php生成图片验证码.pdf

    PHP生成图片验证码技术详解 PHP 生成图片验证码是 Web 开发中常用的安全机制,用来防止机器人程序的恶意攻击。下面我们将详细介绍 PHP 生成图片验证码的技术实现。 session_start() 函数 在 PHP 中,session_...

    PHP页面跳转实现延时跳转的方法

    php在用header重定向的时候,可以设置下... 您可能感兴趣的文章:PHP生成唯一订单号的方法汇总php生成唯一的订单函数分享PHP生成唯一订单号用HTML/JS/PHP方式实现页面延时跳转的简单实例PHP如何实现订单的延时处理详解

    编写php应用程序实现摘要式身份验证的方法详解

    例如下边的代码使用header()函数要求客户端使用Digest验证,它在HTTP消息报头中增加了一个WWW-Authenticate字段:header(‘WWW-Authenticate:Digest Realm=”MyRealm”,nonce=”47alf7cf25ce7″,algorithm=MD5,qop=...

    PHP输出缓存ob系列函数详解

    下面我说说ob的基本作用: 1)防止在浏览器有输出之后再使用setcookie()、header()或session_start()等发送头文件的函数造成的错误。其实这样的用法少用为好,养成良好的代码习惯。 2)捕捉对一些不可获取的函数的...

    PHP封装curl的调用接口及常用函数详解

    如下所示: &lt;?php /** * @desc 封装curl的调用接口,post的请求方式 */ function doCurlPostRequest($url, $requestString, $timeout = ... curl_setopt($con, CURLOPT_HEADER, false); curl_setopt($con,

    PHP mysqli_free_result()与mysqli_fetch_array()函数详解

    PHP mysqli_free_result()与mysqli_fetch_array()函数 mysql_free_result() 仅需要在考虑到返回很大的结果集时会占用多少内存时调用。在脚本结束后所有关联的内存都会被自动释放。 在我们执行完SELECT语句后,释放...

    PHP 输出缓存详解

    输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生影响,只对那些类似于 echo() 和 PHP 代码的数据块有作用。 我们先举一个简单的例子,让大家对Output Control有一个大致的印象: Example 1. ...

Global site tag (gtag.js) - Google Analytics