GET requests should be idempotent and POST requests are generally not. This means that data in $_GET and $_POST should generally be used in different ways.
如果您的应用程序正在使用来自 $_REQUEST的数据,那么它对 GET 和 POST 请求的行为将是相同的,这违反了 GET 的幂等性。
I actually like using it. It gives you the flexibility to use GET or POST which can come in handy for things like search forms where most of the time data is POSTed, but sometimes you'll want to say link to a particular search, so you can use GET parameters instead.
另外,如果您查看许多其他语言(例如 ASP.NET) ,它们根本不区分 GET 和 POST 变量。
另外,将内容加载到 REQUEST 中的顺序由 php.ini 中的配置参数(variable _ order 和 REQUEST _ order)控制。因此,如果通过 POST 和 GET 传入相同的变量,那么实际进入 REQUEST 的变量取决于这些 ini 设置。如果您依赖于特定的顺序,并且这些设置的配置与您预期的不同,那么这可能会影响可移植性。
对于通常通过 GET 提交的普通幂等请求,存在这样一种可能性,即您想要的数据量无法放入 URL 中,因此作为一个实际问题,它已经被突变为一个 POST 请求。
对于具有实际效果的请求,必须检查它是否由 POST 方法提交。但是这样做的方法是显式地检查 $_SERVER['REQUEST_METHOD'],而不是依赖于对于 GET 来说 $_POST是空的。无论如何,如果该方法是 POST,您仍然可能希望从 URL 中提取一些查询参数。
No, the problem with $_REQUEST is nothing to do with conflating GET and POST parameters. It's that it also, by default, includes $_COOKIE. And cookies really aren't like form submission parameters at all: you almost never want to treat them as the same thing.
You can change this behaviour to the much more sensible GP (no C) order with the Request _ order config in PHP 5.3. Where this is not possible, I personally would avoid $_REQUEST and, if I needed a combined GET+POST array, create it manually.
知道你的数据应该从哪里来。参考我上面的例子,允许通过 GET 或 POST 发送响应格式变量是完全合理的。我还允许通过任一方法发送“ action”变量。然而,the actions themselves have very specific requirements as to which HTTP Verb is acceptable。例如,对服务使用的数据进行更改的函数只能通过 POST 发送。对某些类型的非或低特权数据(例如动态生成的地图图像)的请求可以响应来自这两种方法的请求。
variable_order Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set.
我们移除的这种东西越多,人们就越难移除
迅速转向更新、更快、更安全的 PHP 版本
给每个人带来的挫折远远超过一些“丑陋”的遗产
如果有合适的技术原因、性能或
安全,那么我们需要认真审查它。在这种情况下,
thing we should be looking at isn't whether we should remove $_REQUEST
而是我们是否应该从中删除 cookie 数据
already do that, including all of my own, and there is a strong valid
$_ REQUEST 中不包含 cookie 的安全原因
$_ REQUEST 表示 GET 或 POST,没有意识到它也可以包含
像 Cookie 这样的坏人可能会做一些 Cookie 注射
欺骗和破坏幼稚的应用程序。
如果您知道需要什么数据,那么应该显式地请求它。IMO,GET 和 POST 是两种不同的动物,我想不出一个好的理由为什么你需要混合后数据和查询字符串。如果有人有的话,我会很感兴趣的。
当您的脚本可能以相同的方式响应 GET 或 POST 时,使用 $_ REQUEST 会很方便。但我认为这种情况应该是极其罕见的,在大多数情况下,两个单独的函数处理两个单独的概念,或者至少检查方法并选择正确的变量,是首选的。当不需要交叉引用变量的来源时,程序流通常更容易跟踪。对那些必须在6个月内维护您的代码的人友好一些。可能是你。
除了 REQUEST 变量中的 cookie 和环境变量引起的安全问题和 WTF 之外(不要让我从 GLOBAL 开始) ,考虑一下如果 PHP 开始本机支持其他方法,比如 PUT 和 DELETE,将来会发生什么。虽然将它们合并到 REQUEST superglobal 中的可能性极小,但是它们可以作为 on 选项包含在 variable _ order 设置中。因此,您真的不知道 REQUEST 持有什么,什么是优先级,特别是当您的代码部署在第三方服务器上时。
Is POST safer than GET? Not really. It's better to use GET where practical because it's easier to see in your logs how your application is being exploited when it gets attacked. POST is better for operations that affect domain state because spiders generally don't follow them, and predictive fetching mechanisms won't delete all your content when you log into your CMS. However, the question was not about the merits of GET vs POST, it was about how the receiver should treat the incoming data and why it's bad to merge it, so this is really just a BTW.
// delete $_REQUEST when program execute, the program would be lighter
// when large text submitted
unset($_REQUEST);
// wrapper function to get request var
function GetRequest($key, $default = null, $source = '')
{
if ($source == 'get') {
if (isset($_GET[$key])) {
return $_GET[$key];
} else {
return $default;
}
} else if ($source == 'post') {
if (isset($_POST[$key])) {
return $_POST[$key];
} else {
return $default;
}
} else if ($source == 'cookie') {
if (isset($_COOKIE[$key])) {
return $_COOKIE[$key];
} else {
return $default;
}
} else {
// no source specified, then find in GPC
if (isset($_GET[$key])) {
return $_GET[$key];
} else if (isset($_POST[$key])) {
return $_POST[$key];
} else if (isset($_COOKIE[$key])) {
return $_COOKIE[$key];
} else {
return $default;
}
}
}
Darren Cook: "Since php 5.3 the default php.ini says only GET and POST data are put into $_REQUEST. See Php.net/request_order I just
在期待 cookie 时无意中发现了这个向后兼容性中断
数据放在 $_REQUEST里,想知道为什么它不起作用!”
This avoids the cookie problem and gives you at worst an empty array and at best a merger of $_GET and $_POST with the latter taking precedence. If you are not too bothered with allowing URL injection of parameters through the query string, it's quite convenient.