header()
发送一个原始 HTTP 头,用于跳转、设置内容类型、缓存控制。
header(string $header, bool $replace = true, int $response_code = 0): void
参数
| 参数 | 类型 | 说明 |
|---|---|---|
$header | string | 头内容,如 Location: /login/ |
$replace | bool | 是否替换同类头 |
$response_code | int | 同时设置响应码 |
返回值
void — 无返回值。
示例
header('Content-Type: application/json; charset=utf-8');
header('Location: /login/', true, 302);
exit;注意
- header 之前不能有任何输出,包括 BOM 和空行。
- 跳转后必须 exit,否则后面的代码还会执行。
- 设置响应码也可以直接 http_response_code(404)。