You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于HTTP GET与POST方法的困惑:POST请求为何能获取GET参数

Why You Can Access Both $_GET and $_POST in a POST Request

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 $_GET superglobal 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 $_POST when the request method is POST (and the content type is application/x-www-form-urlencoded or multipart/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:

  1. The server sees the URL query string getname=someget and populates $_GET['getname'].
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:58:02