php中header的用法是什么

在PHP中,header函数用于发送原始的HTTP头部。

语法:header(string $string, bool $replace = true, int $http_response_code = null)

参数:

$string: 必需,规定要发送的原始 HTTP 报头。
$replace: 可选,指定是否替换之前发送的相同类型的报头。默认为true,表示替换。
$http_response_code: 可选,设置 HTTP 响应代码。

示例用法:

设置 Content-Type

header("Content-Type: application/json");

重定向到指定网址

header("Location: https://example.com");

设置 HTTP 响应代码

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

注意事项:

header函数必须在任何实际输出之前调用,包括任何HTML标签、空格和换行等。
如果在header函数之前已经有输出,如echo或print等,那么header函数将会失败。
如果要发送多个报头,可以调用多次header函数。

更多详细信息,请参考PHP官方文档:https://www.php.net/manual/zh/function.header.php

阅读剩余
THE END