Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了与环境无关的301/302重定向与PHP大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我已经运行了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 %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头的任何特殊处理,因为它不用于模块通信.

所以通过执行上面的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

大佬总结

以上是大佬教程为你收集整理的与环境无关的301/302重定向与PHP全部内容,希望文章能够帮你解决与环境无关的301/302重定向与PHP所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: