我已经运行了FastCGI的accidental usage状态标题.在环境无关的脚本中是否有使用它的优缺点?
header('Location: ' . $url,true,301);
单独对Apache 2.2而言,我没有任何问题(根据PHPinfo(),服务器使用FastCGI).
您已经拥有的解决方案是最环保的方式.唯一的添加将是重定向后立即退出语句,以确保脚本终止.
让我们来看看在引擎盖下更加紧密的发生什么.
header('Location: ' . $url,301);
exit;
将调用ext / standard / head.c中的C代码
PHP_FUNCTION(header)
{
[ code that just parses the arguments omitted ]
sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD,&ctr);
}
这将依次调用main / SAPI.c中的sapi_header_op函数
[ ... ]
switch (op) {
[ ... ]
case SAPI_HEADER_ADD:
case SAPI_HEADER_REPLACE:
case SAPI_HEADER_DELETE: {
sapi_header_line *p = arg;
if (!p->line || !p->line_len) {
return FAILURE;
}
header_line = p->line;
header_line_len = p->line_len;
http_response_code = p->response_code;
break;
}
[ code that splits header line by colon,trims whitespace etc ]
[ special headers handling code,including setting 302 if Location ]
if (http_response_code) {
sapi_update_response_code(http_response_code);
}
sapi_header_add_op(op,&sapi_header);
return SUCCESS;
如果FastCGI后端正在使用,则添加的标头最终将由sapi / cgi / cgi_main.c中的sapi_cgi_send_headers函数发出
[ ... ]
if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
{
[ emit status line if cgi.rfc2616-headers is set ]
[ Handle a case where there is a user supplied status line ]
[ Handle a case where there is already a user supplied status header ]
[ if none of the above ]
if (err->str) {
len = slprintf(buf,sizeof(buf),"Status: %d %s\r\n",SG(sapi_headers).http_response_code,err->str);
} else {
len = slprintf(buf,"Status: %d\r\n",SG(sapi_headers).http_response_code);
}
[ ... ]
}
[ ... ]
请注意,sapi / apache2handler / sapi_apache2.c中的PHP_apache_sapi_send_headers函数没有对Status头的任何特殊处理,因为它不用于模块通信.
> HTTP状态行中的响应代码被强制为301
>添加位置标题或替换现有标题
>脚本退出,所以没有后续的代码可以更改状态或标题
所有操作都在SAPI层中执行,SAPI层是HTTP服务器适配器(FastCGI,Apache模块等)之上的抽象层.这是跨越环境和可靠的.
历史上,FastCGI中出现了错误,阻止了301个响应正常工作,但这些错误在Web服务器实现中,并且没有什么可以从PHP代码中解决问题.
也可以看看:
> https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Response_message
> https://github.com/php/php-src/blob/a8d38674b1d75de54bb1d3d1436da759ef8e64d7/ext/standard/head.c
> https://github.com/php/php-src/blob/ebb6f5eae6b37b0202eff325b05932f0b1c28944/main/SAPI.c#L841
> https://github.com/php/php-src/blob/ebb6f5eae6b37b0202eff325b05932f0b1c28944/sapi/cgi/cgi_main.c#L405
> https://github.com/php/php-src/blob/ebb6f5eae6b37b0202eff325b05932f0b1c28944/sapi/apache2handler/sapi_apache2.c#L151
> php 301 redirects actually doing a 302 redirect