检查请求是否在 PHP 中回发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4242035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:23:33  来源:igfitidea点击:

Checking if request is post back in PHP

php

提问by Jiew Meng

How can I check if the request is a post back in PHP, is the below ok?

我如何检查请求是否是 PHP 中的回发,以下是否可以?

if (isset($_POST["submit"]))

where submitis the nameof the <input type="submit" />

其中submitname<input type="submit" />

回答by BoltClock

That will work if you know and expect such a submit button on the same page.

如果您知道并期望在同一页面上有这样的提交按钮,这将起作用。

If you don't immediately know anything about the request variables, another way is to check the request method:

如果您对请求变量一无所知,另一种方法是检查请求方法:

if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')

As pointed out in the comments, to specifically check for a postbackand not just any POST request, you need to ensure that the referrer is the same page as the processing page. Something like this:

正如评论中所指出的,要专门检查回发而不仅仅是任何 POST 请求,您需要确保引用页面与处理页面相同。像这样的东西:

if (basename($_SERVER['HTTP_REFERER']) == $_SERVER['SCRIPT_NAME'])

回答by Steven

You want $_SERVER['REQUEST_METHOD'] == 'POST'.

你要$_SERVER['REQUEST_METHOD'] == 'POST'

Yours is a very similar although less general question than this one.

你是一个非常相似但比这个问题更不普遍的问题。

This is probably a better approach than actually checking a post variable. For one, you don't know whether that variable will be sent along. I have the hunch that some browsers just won't send the key at all if no value is specified. Also, I'd worry that some flavors of PHP might not define $_POSTif there are no POSTed values.

这可能是比实际检查 post 变量更好的方法。一方面,您不知道该变量是否会一起发送。我有一种预感,如果没有指定值,某些浏览器根本不会发送密钥。另外,我担心某些风格的 PHP 可能无法定义$_POST是否没有 POSTed 值。

回答by dejjub-AIS

If you want to have a generic routine without dependency "method" (post/get) and any other names of the forum elements, then I recommend this

如果你想要一个没有依赖“方法”(post/get)和论坛元素的任何其他名称的通用例程,那么我推荐这个

<?php
$isPostBack = false;

$referer = "";
$thisPage = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

if (isset($_SERVER['HTTP_REFERER'])){
    $referer = $_SERVER['HTTP_REFERER'];
}

if ($referer == $thisPage){
    $isPostBack = true;
} 
?>

now the if $isPostBack will be true if it is a postback, false if not.

现在 if $isPostBack 如果是回发则为真,否则为假。

I hope this helps

我希望这有帮助

回答by Valentin Flachsel

Yes, that should do it.

是的,应该这样做。

Careful when you're using imagetype submits, they won't send the nameattribute in some browsers and you won't be able to detect the POST. Smashed my head against the desk a few times until I realized it myself.

当您使用image类型提交时要小心,它们不会name在某些浏览器中发送该属性,并且您将无法检测到 POST。把我的头撞在桌子上几次,直到我自己意识到。

The workaround for that is to add a hiddentype input as well.

解决方法是添加一个hidden类型输入。

回答by Inigoesdr

Yes. You could also use if(array_key_exists('submit', $_POST))

是的。你也可以使用if(array_key_exists('submit', $_POST))