功能重定向的HTTP状态 [英] HTTP status for functional redirect

查看:105
本文介绍了功能重定向的HTTP状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我们已经有了显示UI元素的网页,以及只处理表单提交的网页,然后重定向回UI页面。他们使用PHP的header()函数执行此操作:

Right now we've got web pages that show UI elements, and web pages that just process form submissions, and then redirect back to the UI pages. They do this using PHP's header() function:

header("Location: /other_page.php");

这会导致发送302 Found响应;根据HTTP 1.1规范,302适用于请求的资源暂时驻留在不同的URI下的情况。 [HTTP 1.1 spec]

This causes a 302 Found response to be sent; according to the HTTP 1.1 spec, 302 is for cases where "The requested resource resides temporarily under a different URI." [HTTP 1.1 spec]

从功能上讲,这很好,但看起来这似乎是我们正在做的正确的状态代码。看起来303(See Other)在这里是合适的状态,所以我想知道是否有任何理由不使用它。我们必须更明确地使用header(),因为我们必须指定状态行而不仅仅是Location:字段。想法?

Functionally, this is fine, but it doens't seem like this is the proper status code for what we're doing. It looks like 303 ("See Other") is the appropriate status here, so I'm wondering if there's any reason not to use it. We'd have to be more explicit in our use of header(), since we'd have to specify that status line rather than just a Location: field. Thoughts?

推荐答案

您可以使用其中任何一个,但用于重定向后的正确状态代码为303.

You can use either, but the proper statuscode to use for redirect-after-post is 303.

这种混淆有一个历史性的解释。最初,302指定浏览器不得更改重定向请求的方法。这使得它不适合您希望浏览器发出GET请求的重定向后发布。但是,所有浏览器似乎都错误地解释了规范并始终发出GET请求。为了消除歧义,HTTP / 1.1指定了两个新代码:303和307.303基本上指定302的事实解释,而307指定302的原始规范。因此在实践中302和303是可互换的,并且在理论302和307是​​。

The confusion has a historical explanation. Originally, 302 specified that the browser mustn't change the method of the redirected request. This makes it unfit for redirect-after-post, where you want the browser to issue a GET request. However, all browsers seems to misinterpret the specs and always issue a GET request. In order to clear up the ambiguity HTTP/1.1 specified two new codes: 303 and 307. 303 essentially specifies the de-facto interpretation of 302, while 307 specifies the original specification of 302. So in practice 302 and 303 are interchangeable, and in theory 302 and 307 are.

如果你真的关心兼容性,那么302比303更安全,因为HTTP / 1.0代理可能不是理解303,但所有现代浏览器都讲HTTP / 1.1,所以这不是一个真正的问题。我建议使用303,因为这是最正确的事情。

If you really care about compatibility, 302 is a safer bet than 303, since HTTP/1.0 agents may not understand 303, but all modern browsers speak HTTP/1.1, so it isn't a real issue. I would recommend using 303, since that's the most correct thing to do.

在旁注; 位置字段应为完整网址。在实践中它并不重要 - 浏览器是宽容的 - 但如果你关心规格,这是正确的事情。

On a side-note; The Location field should be a full URL. In practice it doesn't matter - browsers are forgiving - but if you care about the specs, that's the proper thing to do.

这篇关于功能重定向的HTTP状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆