写给愿意动手的 PHP 开发者
PHP学习网 PHP学习网
公告PHP学习网为公益性网站,致力于为广大PHP爱好者服务
网络与请求 PHP PHP 4 header

header()

发送一个原始 HTTP 头,用于跳转、设置内容类型、缓存控制。

header(string $header, bool $replace = true, int $response_code = 0): void

参数

参数类型说明
$headerstring头内容,如 Location: /login/
$replacebool是否替换同类头
$response_codeint同时设置响应码

返回值

void — 无返回值。

示例

PHP
header('Content-Type: application/json; charset=utf-8');
header('Location: /login/', true, 302);
exit;

注意

  • header 之前不能有任何输出,包括 BOM 和空行。
  • 跳转后必须 exit,否则后面的代码还会执行。
  • 设置响应码也可以直接 http_response_code(404)。