加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

与环境无关的301/302重定向与PHP

发布时间:2020-12-13 21:24:23 所属栏目:Nginx 来源:网络整理
导读:我已经运行了FastCGI的accidental usage状态标题.在环境无关的脚本中是否有使用它的优缺点? header('Location: ' . $url,true,301); 单独对Apache 2.2而言,我没有任何问题(根据phpinfo(),服务器使用FastCGI). 该脚本针对Apache和nginx(mod_php和FastCGI).什

我已经运行了FastCGI的accidental usage状态标题.在环境无关的脚本中是否有使用它的优缺点?

header('Location: ' . $url,true,301);

单独对Apache 2.2而言,我没有任何问题(根据phpinfo(),服务器使用FastCGI).

该脚本针对Apache和nginx(mod_php和FastCGI).什么是防故障解决方案?

最佳答案
HTTP状态代码作为HTTP响应的第一行的一部分发出.根据Fast CGI FAQ,Status头是由服务器识别的特殊头部,它控制这一行,并且不发送到客户端.但是如果它与非FastCGI服务器适配器一起使用,服务器将忽略该值,并且可以发送头.

您已经拥有的解决方案是最环保的方式.唯一的添加将是重定向后立即退出语句,以确保脚本终止.

让我们来看看在引擎盖下更加紧密的发生什么.

以下PHP重定向代码

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 %srn",SG(sapi_headers).http_response_code,err->str);
        } else {
            len = slprintf(buf,"Status: %drn",SG(sapi_headers).http_response_code);
        }
     [ ... ]
}
[ ... ]

请注意,sapi / apache2handler / sapi_apache2.c中的php_apache_sapi_send_headers函数没有对Status头的任何特殊处理,因为它不用于模块通信.

所以通过执行上面的PHP代码

> 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读