php中header的用法是什么

   2025-01-31 5810
核心提示:在PHP中,header函数用于发送原始的HTTP头部。语法:header(string $string, bool $replace = true, int $http_response_code =

在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

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言