简体   繁体   English

$ _SERVER ['REQUEST_METHOD']默认情况下评估为'GET'

[英]$_SERVER['REQUEST_METHOD'] evaluates to to 'GET' by default

Been doing a bit of digging about this, but, no luck finding information 对此进行了一些挖掘,但是,没有运气找到信息

I'm trying to check whether a form has been submitted and if it is either GET or POST . 我正在尝试检查表单是否已提交,以及是否为GETPOST So essentially I use: 基本上我用的是:

if($_SERVER['REQUEST_METHOD'] == 'GET')

or 要么

if($_SERVER['REQUEST_METHOD'] == 'POST')

However, I find that if I don't submit any form, and just go to the page directly - a simple HTTP Request, the REQUEST_METHOD is GET . 但是,我发现如果我不提交任何表单,只是直接转到页面 - 一个简单的HTTP请求, REQUEST_METHOD就是GET What gives? 是什么赋予了? Is this by design? 这是设计的吗? If so then I can't use the former statement to check whether a form has been submitted via GET. 如果是,那么我不能使用前一个语句来检查表单是否已通过GET提交。 Seems a bit redundant... 似乎有点多余......

Someone with a bit more knowledge please explain this to me, that would be appreciated. 有更多知识的人请向我解释一下,我将不胜感激。 Thanks. 谢谢。

Basically most HTTP requests are GET requests. 基本上大多数HTTP请求都是GET请求。

you can use if($_POST) to check if it's a POST. 您可以使用if($_POST)来检查它是否是POST。 (That's the array with POST data in it. All pages have $_GET set, so if($_GET) won't work to tell if it's a GET) (这是包含POST数据的数组。所有页面都设置了$_GET ,所以if($_GET)无法判断它是否为GET)

However, if(count($_GET)>0) will tell you if there is $_GET data. 但是, if(count($_GET)>0)将告诉您是否有$ _GET数据。

You can have both POST and GET data though, by sending a POST request to a URL with GET data in it (ie http://example.unreal?GetData=4&OtherData=no ) 您可以同时拥有POST和GET数据,方法是向其中包含GET数据的URL发送POST请求(即http://example.unreal?GetData=4&OtherData=no

只需通过浏览器访问页面或单击链接即可获得GET请求。

I have encountered same problem and resolved by this: When the form action is set as iprofile?r=search , the request method is always GET and all input data in the form are lost. 我遇到了同样的问题并解决了这个问题:当表单操作设置为iprofile?r=search ,请求方法总是GET,表单中的所有输入数据都将丢失。 But when i set the action to iprofile/?r=search , the request method become POST. 但是当我将动作设置为iprofile/?r=search ,请求方法变为POST。

Maybe you can also check your action URL. 也许您也可以查看您的操作网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM