简体   繁体   中英

$_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 . 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 . 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. 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.

you can use if($_POST) to check if it's a 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)

However, if(count($_GET)>0) will tell you if there is $_GET data.

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 )

只需通过浏览器访问页面或单击链接即可获得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. But when i set the action to iprofile/?r=search , the request method become POST.

Maybe you can also check your action URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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