简体   繁体   English

$ _SERVER ['REQUEST_METHOD']仍然可行吗?

[英]Is $_SERVER['REQUEST_METHOD'] still viable?

So, for a while now I've been using the following to check if my post data has been set. 所以,有一段时间我一直在使用以下内容来检查我的帖子数据是否已设置。

if( ! empty( $_POST ) ) { }

But recently I've been seeing a lot of posts saying that the above is a "hack" and the below is the correct "better" way. 但最近我看到很多帖子说上面是“黑客”,下面是正确的“更好”的方式。

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

By recently I just mean I've only recently found it. 到最近,我只是说我最近才找到它。 All the posts that are discussing this later method are from 2009'ish. 正在讨论这个后来方法的所有帖子都来自2009年。 A bit old by coding standards so I figure its ok to get a fresh opinion on this topic. 编码标准有点旧,所以我觉得可以对这个主题有一个新的看法。

I've come to understand that the two methods are different. 我明白这两种方法是不同的。 The first is considered a "hack" that just checks if the post array has been set, which will happen if a post request is made. 第一个被认为是“hack”,只检查是否已经设置了post数组,如果发出了post请求,就会发生这种情况。 The second actually checks the server to see if a post request has been made. 第二个实际上检查服务器以查看是否已发出发布请求。 I imagine the second might be a little more secure, but if the information is cleansed anyways I don't see how it makes much of a difference. 我想第二个可能会更安全,但如果信息被清理干净,我看不出它有多大区别。

I've also seen posts that the later was only used in PHP versions <= 4 because PHP was still using the $_REQUEST global at this point and this was the way PHP coders used to determine the source of certain request paramters. 我还看到后者仅用于PHP版本<= 4的帖子,因为PHP此时仍然使用$_REQUEST全局,这是PHP编码器用于确定某些请求参数的来源的方式。 I'm not sure how accurate that last statement is, because the questions being posed in the older posts are the same as mine. 我不确定最后一个陈述是多么准确,因为旧帖子中提出的问题与我的相同。 They use the post global and not request. 他们使用post global而不是request。 However, this is a more recent post than any of the others (2011), and from a source I've come to trust. 然而,这是一个比其他任何一个(2011年)更新的帖子,并且从我开始信任的来源。 So I'm not sure what to make of it. 所以我不知道该怎么做。

And what to do when checking for get? 检查get时该怎么办? I've seen a couple of places say that the server request method doesn't seem to work in this instance, and I can only assume that it is because post supercedes get and the request method can only hold one paramter. 我已经看到有几个地方说服务器请求方法在这个实例中似乎不起作用,我只能假设它是因为post superscedes get而请求方法只能容纳一个参数。 So if you have both post and get data what do you do? 所以,如果您同时发布和获取数据,您会做什么? A comment on one of these posts suggests using the request global instead of post and get, but I've been under the impression that is a bad idea. 对其中一篇帖子的评论建议使用全局请求而不是发布和获取,但我一直认为这是一个坏主意。

This is the most recent source I could find, and I did so by looking through the similar questions on the side before submitting. 是我能找到的最新资料,我在提交之前通过查看侧面的类似问题这样做了。 It is specifically asking about using a submit value to check if the form was passed, but it does also mention the request method. 它特别询问使用提交值来检查表单是否已通过,但它也提到了请求方法。 A lot of it seems to indicate that the later is still being used commonly. 很多似乎表明后者仍然被普遍使用。 So is this advice still valid? 这个建议仍然有效吗? Is checking the request method still the best option? 检查请求方法仍然是最佳选择吗?

Yes, it's still there, and it's still 100% reliable. 是的,它仍然存在,它仍然100%可靠。 The $_SERVER["REQUEST_METHOD"] var is set by PHP itself, based on the actual request method used by the user's connection. $_SERVER["REQUEST_METHOD"] var由PHP本身设置,基于用户连接使用的实际请求方法。 A user can't send in a query paramter or otherwise influence the value of that var, other than by changing the type of request. 除了通过更改请求的类型之外,用户不能发送查询参数或以其他方式影响该var的值。

Your if(!$_POST) is not reliable, because it IS possible to perform a post but not send any data across it, eg: if(!$_POST)是不可靠的,因为它有可能执行后,但不能跨越它发送任何数据,例如:

<form method="post">
<input type="submit" />
</form>

will produce just such an empty $_POST array - there are no named form elements in the form, so no data will be sent, yet a POST has still be performed. 将生成这样一个空的$ _POST数组 - 表单中没有命名的表单元素,因此不会发送任何数据,但仍然会执行POST。

I would not worry about PHP4 not having this superglobal. 我不担心PHP4没有这个超全球。 PHP 4 is a stone age version, and code which supports v4 but built on v5 will have to contain so many ugly/disgusting hacks to achieve backwards compatibility that anyone having to work on that code would suffer from nightmares. PHP 4是一个石器时代版本,支持v4但基于v5构建的代码必须包含许多丑陋/恶心的黑客以实现向后兼容性,任何必须处理该代码的人都会遭受噩梦。 PHP 4 should be considered dead and gone. PHP 4应该被认为已经死了。

I always use the $_SERVER['REQUEST_METHOD']; 我总是使用$_SERVER['REQUEST_METHOD']; variable to check the request method. 变量来检查请求方法。

This variable also says if the request is a ' GET ', ' HEAD ', ' POST ' or ' PUT ' request. 该变量还表示请求是' GET ',' HEAD ',' POST '还是' PUT '请求。

http://php.net/manual/en/reserved.variables.server.php http://php.net/manual/en/reserved.variables.server.php

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

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