php 的 header 函数有什么作用?
what does php's header function do?
我找不到这个问题的任何解决方案。
我很难处理这段代码。根据我搜索的内容:
HTTP/1.1 200 OK
- 表示页面良好或正常。
我不明白这个 header 功能部分而已。
它实际上是代码的一部分。
我的问题是:
为什么这个代码发送header('HTTP/1.1 200 OK');
?我知道这个代码意味着页面是好的,但我们为什么要发送这个代码?
什么是 cache-control 部分,如果代码发送该部分会发生什么?
什么是 Expires:
,日期是 1970 年? (请简单说明)
如果代码发送 header ('Content-type: application/json');
这部分会发生什么,我们为什么要发送这个?
代码在这里:
function json_response( $data, $error=false ) {
if( $error )
header('HTTP/1.1 500 JSON Error');
else
header('HTTP/1.1 200 OK');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
header('Content-type: application/json');
// Convert strings/integers into an array before outputting data...
if(!is_array($data))
echo json_encode(array($data), true);
else
echo json_encode($data, true);
exit;
}
您只需 return 向浏览器告知您拥有类型为 json (header('Content-type: application/json');
) 的内容,该内容将在检索 (header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
) 后直接过期。如果您的浏览器没有使已收到的代码过期并再次请求它,您会说不应从缓存中使用这些代码。相反,它应该再次从服务器检索 (header('Cache-Control: no-cache, must-revalidate');
)。
header('HTTP/1.1 200 OK');
只是为了防止在您的代码前面设置了另一个 header(我认为)。通常如果没有设置header,这个可以省略。
代码returns来自脚本的json格式数据。
如果没有错误 HTTP/1.1 200 OK 由 header 返回,这意味着数据打印到页面。
Cache-control 表示您请求的数据无法写入内存 - 也就是说,每次加载页面时,您都必须重新加载从页面获取的数据。
Expires - 我想它存在的原因与 Cache-control 相同,如果过期日期总是过去,这意味着,每次访问页面时,您都会重新加载它提供给您的所有数据。
Application/json指定脚本返回的数据应该被视为JSON类型,什么是JSON,嗯google那个。
why this code is sending "header('HTTP/1.1 200 OK');"? i know this code means that the page is Good, but why are we sending this code??_
这会告诉您的浏览器已找到所请求的脚本。然后浏览器可以假设它也会获得一些其他数据。
what is cache-control part?? and what will happen if the code send that?_
这告诉浏览器和中间缓存,不要缓存我发送的数据。这样一来,当您再次请求此数据时,它将不得不转到您的服务器并重新运行数据收集过程,而不是从浏览器缓存或互联网上某处的中间缓存中获取数据,在您的浏览器和你的服务器。
what is "Expires:"? and the date is 1970? (please simple explanation)_
这又是为了缓存控制。是说缓存应该在 1970 年过期,换句话说,如果你缓存了它,你应该删除它,因为 1970 年已经很久了。
what will happen if the code send header('Content-type: application/json'); this part? and why are we sending this ???_
这是告诉浏览器您发送的数据是 JSON 格式,因此如何处理它,在您的情况下,这意味着转换要发送到的 JSON 字符串javascript 对象,因此 javascript 代码可以将其作为本机对象处理,而不必手动将 JSON 字符串转换为 Javascript 对象。
我找不到这个问题的任何解决方案。
我很难处理这段代码。根据我搜索的内容:
HTTP/1.1 200 OK
- 表示页面良好或正常。
我不明白这个 header 功能部分而已。
它实际上是代码的一部分。
我的问题是:
为什么这个代码发送
header('HTTP/1.1 200 OK');
?我知道这个代码意味着页面是好的,但我们为什么要发送这个代码?什么是 cache-control 部分,如果代码发送该部分会发生什么?
什么是
Expires:
,日期是 1970 年? (请简单说明)如果代码发送 header
('Content-type: application/json');
这部分会发生什么,我们为什么要发送这个?
代码在这里:
function json_response( $data, $error=false ) {
if( $error )
header('HTTP/1.1 500 JSON Error');
else
header('HTTP/1.1 200 OK');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
header('Content-type: application/json');
// Convert strings/integers into an array before outputting data...
if(!is_array($data))
echo json_encode(array($data), true);
else
echo json_encode($data, true);
exit;
}
您只需 return 向浏览器告知您拥有类型为 json (header('Content-type: application/json');
) 的内容,该内容将在检索 (header('Expires: Mon, 01 Jan 1970 00:00:00 GMT');
) 后直接过期。如果您的浏览器没有使已收到的代码过期并再次请求它,您会说不应从缓存中使用这些代码。相反,它应该再次从服务器检索 (header('Cache-Control: no-cache, must-revalidate');
)。
header('HTTP/1.1 200 OK');
只是为了防止在您的代码前面设置了另一个 header(我认为)。通常如果没有设置header,这个可以省略。
代码returns来自脚本的json格式数据。
如果没有错误 HTTP/1.1 200 OK 由 header 返回,这意味着数据打印到页面。
Cache-control 表示您请求的数据无法写入内存 - 也就是说,每次加载页面时,您都必须重新加载从页面获取的数据。
Expires - 我想它存在的原因与 Cache-control 相同,如果过期日期总是过去,这意味着,每次访问页面时,您都会重新加载它提供给您的所有数据。
Application/json指定脚本返回的数据应该被视为JSON类型,什么是JSON,嗯google那个。
why this code is sending "header('HTTP/1.1 200 OK');"? i know this code means that the page is Good, but why are we sending this code??_
这会告诉您的浏览器已找到所请求的脚本。然后浏览器可以假设它也会获得一些其他数据。
what is cache-control part?? and what will happen if the code send that?_
这告诉浏览器和中间缓存,不要缓存我发送的数据。这样一来,当您再次请求此数据时,它将不得不转到您的服务器并重新运行数据收集过程,而不是从浏览器缓存或互联网上某处的中间缓存中获取数据,在您的浏览器和你的服务器。
what is "Expires:"? and the date is 1970? (please simple explanation)_
这又是为了缓存控制。是说缓存应该在 1970 年过期,换句话说,如果你缓存了它,你应该删除它,因为 1970 年已经很久了。
what will happen if the code send header('Content-type: application/json'); this part? and why are we sending this ???_
这是告诉浏览器您发送的数据是 JSON 格式,因此如何处理它,在您的情况下,这意味着转换要发送到的 JSON 字符串javascript 对象,因此 javascript 代码可以将其作为本机对象处理,而不必手动将 JSON 字符串转换为 Javascript 对象。