在 PHP 中,可以使用 header 函数来设置响应的 response 信息。header 函数用于发送原生的 HTTP 头。

以下是设置常见响应信息的示例:

  1. 设置响应状态码:
header('HTTP/1.1 200 OK');
  1. 设置响应内容类型:
header('Content-Type: text/html');
  1. 设置响应字符编码:
header('Content-Type: text/html; charset=utf-8');
  1. 设置重定向:
header('Location: http://www.example.com');
  1. 设置缓存控制:
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

注意:在调用 header 函数之前,不能有任何输出,包括空白字符。否则会导致'Cannot modify header information'错误。

PHP 设置响应信息 - Header 函数使用指南

原文地址: https://www.cveoy.top/t/topic/l7tj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录