关于HTTP GET与POST方法的困惑:POST请求为何能获取GET参数
Great question! This is a super common misunderstanding about HTTP request methods and parameters—let’s clear it up step by step.
First, a critical correction: HTTP methods and URL query params are independent
You might have thought GET and POST are mutually exclusive when it comes to parameter sources, but that’s not how HTTP works. Here’s the breakdown:
- URL query parameters (the stuff after
?in your URL) are part of the URL itself, regardless of whether you’re sending a GET or POST request. PHP parses these into the$_GETsuperglobal no matter what request method is used. - POST body parameters (your form data) are sent in the request’s body (not the URL), and PHP parses these into
$_POSTwhen the request method is POST (and the content type isapplication/x-www-form-urlencodedormultipart/form-data, which is standard for form submissions).
What’s happening in your test case
When you loaded http://localhost/testing/index.php?getname=someget and then submitted the POST form, your browser sent the POST request to the same URL—including the ?getname=someget query string. So:
- The server sees the URL query string
getname=somegetand populates$_GET['getname']. - The server also sees the POST body data (like
posttitle=...) and populates$_POST['posttitle'].
There’s no conflict here because these two parameter sets come from entirely different parts of the HTTP request.
A quick test to confirm
Try modifying your form’s action attribute to point to index.php (without the query string), then submit it. You’ll find that $_GET['getname'] is no longer available, but $_POST['posttitle'] still is. That proves the query string is purely tied to the URL, not the request method.
Bonus: Can GET requests have body data?
For completeness—yes, technically a GET request can include a body, but HTTP standards discourage it, and PHP won’t parse that body into $_POST (since $_POST is reserved for POST request bodies). But that’s a separate edge case!
内容的提问来源于stack exchange,提问作者Shilan Titaju

