简体   繁体   English

$ _SERVER ['REQUEST_METHOD'] ==='POST'的作用是什么?

[英]What does this $_SERVER['REQUEST_METHOD'] === 'POST' do?

A little new to php and reading some people's code and saw this: php的一些新知识,阅读了一些人的代码,并看到了以下内容:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (isset($_POST['...'])) {

        } else {
            echo "...";
        }
    }

I get what isset(...) is doing but don't understand the what the first if statement. 我知道isset(...)在做什么,但不了解第一个if语句的含义。 Especially because the if statement was working when my request method was GET but wasn't when my request method was POST 尤其是因为当我的请求方法为GET时if语句有效,但当我的请求方法为POST时if语句无效

say your page is called yourpage.php -- What that code means is that the portion of code in the IF statement will ONLY be run if you are accessing yourPage.php page via Posting a form. 假设您的页面称为yourpage.php -该代码的含义是,仅当您通过发布表单访问yourPage.php页面时,才会运行IF语句中的代码部分。 So if you just load that page normally, by typing yourpage.php in the address bar. 因此,如果您只是正常加载该页面,请在地址栏中输入yourpage.php。 that code will not run. 该代码将无法运行。

But if you have some < form action='yourPage.php' >. 但是,如果您有一些<form action ='yourPage.php'>。 When you submit that form, and you come to yourpage.php That code will run only in that instance. 当您提交该表单时,您来到yourpage.php,该代码将仅在该实例中运行。 When the page has come via posting. 页面通过发布到达时。

Its basically a way to ensure that certain code will only happen AFTER the posting of the form, think of a message like "Thanks for filling out our survey!" 这基本上是一种确保某些代码仅在表单发布后才会发生的方法,例如“感谢您填写调查表”之类的消息。 which pops up after you submit your form only, but still on the same page. 仅在您提交表单后弹出,但仍在同一页面上。

$_SERVER['REQUEST_METHOD'] is one of the PHP server variables . $_SERVER['REQUEST_METHOD']PHP服务器变量之一

It determines: 它确定:

Which request method was used to access the page; 使用哪种请求方法访问页面; ie 'GET', 'HEAD', 'POST', 'PUT'. 即“ GET”,“ HEAD”,“ POST”,“ PUT”。

It's generally defaulted to GET though, so don't rely on it for determining if a form has been posted or not (eg if not POST then must be GET etc). 它通常默认为GET,因此不要依赖它来确定是否已过帐表单(例如,如果不是POST,则必须为GET等)。

You said: 你说:

the if statement was working when my request method was GET but wasn't when my request method was POST 当我的请求方法为GET时,if语句有效,但当我的请求方法为POST时,if语句无效

I can't see why that would be the case, you must have had something else different, or possibly caching/browser history was at play. 我不明白为什么会这样,您必须有其他不同之处,或者可能正在使用缓存/浏览器历史记录。

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

相关问题 $ _SERVER [&#39;REQUEST_METHOD&#39;]以及同时POST和GET - $_SERVER['REQUEST_METHOD'] and simultaneous POST and GET $_SERVER['REQUEST_METHOD'] 在服务器上不起作用 - $_SERVER['REQUEST_METHOD'] does not work on server 检查$ _SERVER [&#39;REQUEST_METHOD&#39;] ==&#39;POST&#39;的原因? - Reason for checking if $_SERVER['REQUEST_METHOD'] == 'POST'? $ _SERVER [“ REQUEST_METHOD”] ==“ POST”给出错误 - $_SERVER[“REQUEST_METHOD”] == “POST” giving error $_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot; 中未定义的索引 - Undefined index in $_SERVER["REQUEST_METHOD"] == "POST" if($ _ SERVER [&#39;REQUEST_METHOD&#39;] ==“ POST”)遇到麻烦 - trouble with if($_SERVER['REQUEST_METHOD'] == “POST”) WordPress和处理if(($ _SERVER [“ REQUEST_METHOD”] ==“ POST”) - Wordpress and handling if (($_SERVER[“REQUEST_METHOD”] == “POST”) if($ _ SERVER [&#39;REQUEST_METHOD&#39;] ==&#39;POST&#39;)和android post请求不起作用 - if($_SERVER['REQUEST_METHOD']=='POST') and android post request not working isset($ _ POST [&#39;submit&#39;])不起作用,但是$ _SERVER [“ REQUEST_METHOD”] ==“ POST”起作用 - isset($_POST['submit']) doesn't work but $_SERVER[“REQUEST_METHOD”] == “POST” does isset($ _ POST [&#39;submit&#39;])vs $ _SERVER [&#39;REQUEST_METHOD&#39;] ==&#39;POST&#39; - isset($_POST['submit']) vs $_SERVER['REQUEST_METHOD']=='POST'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM