我可以通过PHP中的header()方法设置请求方法吗? [英] Can I set the request method via header() method in PHP?

查看:63
本文介绍了我可以通过PHP中的header()方法设置请求方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个表单,除了基于表单元素值构建用于重定向"用户的url外,其他任何操作都没做.

I have build a form that do nothing else than build a url to "redirect" the user, based on form element values.

该表单包含分类法中的值,该分类法包含国家(地区)和每个城市的城市.

The form contains values from a taxonomy that contains the Countries and the cities of each city.

因此,当用户选择一个国家/地区时,该表单将生成一个url,将用户发送到显示该国家/地区的所有记录的相应页面.如果用户选择和城市字段,则该表单将生成将用户发送到相应页面的url,该页面显示该国家/地区的记录>城市.

So when the user choosing a country the form building the url that send the user to the appropriate page that display all the records of this country. If the user choose and the city field, then the form building the url that send the user to the appropriate page that display the records of this country > city.

它使用"post"作为方法的形式.

The form it is using "post" as method.

直到这里一切正常!

问题是,当用户尝试刷新结果页面或尝试使用后续页面中的后退按钮时.

The problem is when the user try to refresh the results page, or try to use the back button from a subsequent page.

要解决此问题,在将表单提交到同一表单后,我尝试将用户重定向,但是我担心会出现无休止的重定向.因此,我想知道是否可以通过 header()函数更改修改请求方法.

To fix that, I try to redirect the user after submiting the form to the same form, but I am afraid for endless redirection. For this reason I like to know if it is posible to change the modify the request method via the header() function.

这可以帮助我检查 $ _ SERVER [REQUEST_METHOD] 是否设置为 POST ,从而进行重定向.

This can help me to check the $_SERVER[REQUEST_METHOD] if it is set to POST, thus do, the redirection.

推荐答案

如果您阅读HTTP标头,您将知道 header("Location:...")设置了一个状态代码,该状态代码明确导致浏览器使用 GET 请求,而不是其使用的任何方法.

If you read up on HTTP headers, you'll know that header("Location: ...") sets a status code that explicitly causes the browser to use a GET request instead of whatever method it was using.

实际上,如果要强制浏览器使用相同的方法重新发送请求,则必须手动将状态代码设置为308.

In fact, you have to manually set a status code of 308 if you want to force the browser to resend the request using the same method.

所以,不用担心:)

这篇关于我可以通过PHP中的header()方法设置请求方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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