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

本文详细介绍了PHP中header函数的使用方法,包括页面跳转、设置content-type、响应状态码、延迟跳转、浏览器缓存控制、HTTP验证以及文件下载等。通过实例展示了如何在不同场景下正确使用header函数,帮助开发者更好地理解和应用。
部署运行你感兴趣的模型镜像

void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) : Send a raw HTTP header

下面有一些使用header的几种用法:

1、使用header函数进行跳转页面;

header('Location:'.$url);

其中$url就是将要跳转的url了。

这种用法的注意事项有以下几点:

•Location和":"之间不能有空格,否则会出现错误(注释:我刚测试了,在我本地环境下,没有跳转页面,但是也没有报错,不清楚什么原因);

•在用header前不能有任何的输出(注释:这点大家都知道的,如果header之前有任何的输出,包括空白,就会出现header already sent by xxx的错误);

•header 后面的东西还会执行的;

2、使用header声明content-type

header('content-type:text/html;charset=utf-8');

这个没有什么好说的;

3、使用header返回response 状态码

header(sprintf('%s %d %s', $http_version, $status_code, $description));

样式就是这样的;

例如:header('HTTP/1.1 404 Not Found');

4、使用header在某个时间后执行跳转

header("Refresh: {$delay}; url={$url}");

其中$delay就是推迟跳转的时间,$url为需要跳转的url

例如:header('Refresh: 10; url=http://www.example.org/'); 意思为10s后跳转到http://www.eexample.org这个网站

5、使用header控制浏览器缓存

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");

header("Cache-Control: no-cache, must-revalidate");

header("Pragma: no-cache");

6、执行http验证

header('HTTP/1.1 401 Unauthorized');

header('WWW-Authenticate: Basic realm="Top Secret"');

7、使用header进行下载操作

header('Content-Type: application/octet-stream');//设置内容类型

header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件下载 如果将attachment换成inline意思为在线打开

header('Content-Transfer-Encoding: binary');//设置传输方式

header('Content-Length: '.filesize('example.zip'));//设置内容长度

// load the file to send:

readfile('example.zip');//读取需要下载的文件

下面再给大家介绍PHP header 的几种用法

跳转页面

header('Location:'.$url); //Location和":"之间无空格。

声明content-type

header('content-type:text/html;charset=utf-8');

返回response状态码

header('HTTP/1.1 404 Not Found');

在某个时间后执行跳转

header('Refresh: 10; url=http://www.baidu.com/'); //10s后跳转。

控制浏览器缓存

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");

header("Cache-Control: no-cache, must-revalidate");

header("Pragma: no-cache");

执行http验证

header('HTTP/1.1 401 Unauthorized');

header('WWW-Authenticate: Basic realm="Top Secret"');

执行下载操作

header('Content-Type: application/octet-stream'); //设置内容类型

header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件

header('Content-Transfer-Encoding: binary'); //设置传输方式

header('Content-Length: '.filesize('example.zip')); //设置内容长度

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### PHP 中 `header()` 函数的详细用法与功能说明 `header()` 函数PHP 中用于向客户端发送原始 HTTP 头信息。它必须在任何实际输出之前调用,包括 HTML 标签、空行或空格等[^2]。 #### 1. 基本语法 函数的基本语法如下: ```php void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) ``` - `$string`: 要发送的头信息字符串。 - `$replace`: 可选参数,默认为 `true`,表示替换掉同名的头信息;如果设置为 `false`,则会添加一个新的头信息。 - `$http_response_code`: 可选参数,用于强制设置 HTTP 响应状态码。 #### 2. 常见用法 ##### (1) 重定向页面 通过 `header()` 函数可以实现页面的重定向,例如 301 永久重定向或 302 临时重定向。 ```php header("Location: https://www.example.com", true, 301); // 301永久重定向 ``` 此代码将用户重定向到指定 URL,并设置 HTTP 状态码为 301。 ##### (2) 设置内容类型 可以使用 `header()` 函数来指定返回内容的 MIME 类型。 ```php header("Content-Type: application/json"); // 返回 JSON 数据 header("Content-Type: text/html; charset=UTF-8"); // 返回 HTML 页面并指定字符集 ``` ##### (3) 强制下载文件 通过设置 `Content-Disposition` 头信息,可以让浏览器提示用户下载文件而不是直接打开。 ```php header('Content-Type: application/zip'); // 设置 MIME 类型为 zip 文件 header('Content-Disposition: attachment; filename="example.zip"'); // 强制下载文件名为 example.zip ``` 如果将 `attachment` 替换为 `inline`,则表示在线打开文件[^4]。 ##### (4) 自定义 HTTP 状态码 可以通过 `header()` 函数自定义 HTTP 响应状态码。 ```php header("HTTP/1.1 404 Not Found"); // 返回 404 错误 header("HTTP/1.1 500 Internal Server Error"); // 返回 500 错误 ``` ##### (5) 缓存控制 可以使用 `header()` 函数来控制浏览器缓存行为。 ```php header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // 禁止缓存 header("Pragma: no-cache"); // 兼容旧版浏览器 ``` #### 3. 注意事项 - 必须确保在调用 `header()` 函数之前没有任何输出,否则会导致报错:`headers already sent`。 - 如果需要跨多个文件使用 `header()`,请确保所有文件都没有多余的空格或换行符。 #### 4. 示例代码 以下是一个完整的示例,展示如何使用 `header()` 实现页面重定向和强制下载文件的功能: ```php <?php // 页面重定向 header("Location: https://www.example.com", true, 302); // 强制下载文件 header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="example.txt"'); header('Content-Length: ' . filesize('example.txt')); readfile('example.txt'); ?> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值