This question already has an answer here:
这个问题在这里已有答案:
- Detecting request type in PHP (GET, POST, PUT or DELETE) 11 answers
检测PHP中的请求类型(GET,POST,PUT或DELETE)11个答案
How can I check if the request is a post back in PHP, is the below ok?
如何检查请求是否是PHP中的回复,以下是否可以?
if (isset($_POST["submit"]))
where submit
is the name
of the <input type="submit" />
其中submit是的名称
5 个解决方案
#1
24
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 postback and 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'])
#2
3
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 $_POST
if there are no POSTed values.
这可能是比实际检查post变量更好的方法。首先,您不知道该变量是否会被发送。我有预感,如果没有指定值,某些浏览器根本不会发送密钥。另外,我担心如果没有POSTed值,某些版本的PHP可能无法定义$ _POST。
#3
3
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如果是回发则为true,否则为false。
I hope this helps
我希望这有帮助
#4
1
Yes, that should do it.
是的,应该这样做。
Careful when you're using image
type submits, they won't send the name
attribute 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.
在使用图像类型提交时要小心,他们不会在某些浏览器中发送name属性,您将无法检测到POST。把我的头撞在桌子上几次,直到我自己意识到这一点。
The workaround for that is to add a hidden
type input as well.
解决方法是添加隐藏类型输入。
#5
0
Yes. You could also use if(array_key_exists('submit', $_POST))
是。你也可以使用if(array_key_exists('submit',$ _POST))
#1
24
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 postback and 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'])
#2
3
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 $_POST
if there are no POSTed values.
这可能是比实际检查post变量更好的方法。首先,您不知道该变量是否会被发送。我有预感,如果没有指定值,某些浏览器根本不会发送密钥。另外,我担心如果没有POSTed值,某些版本的PHP可能无法定义$ _POST。
#3
3
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如果是回发则为true,否则为false。
I hope this helps
我希望这有帮助
#4
1
Yes, that should do it.
是的,应该这样做。
Careful when you're using image
type submits, they won't send the name
attribute 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.
在使用图像类型提交时要小心,他们不会在某些浏览器中发送name属性,您将无法检测到POST。把我的头撞在桌子上几次,直到我自己意识到这一点。
The workaround for that is to add a hidden
type input as well.
解决方法是添加隐藏类型输入。
#5
0
Yes. You could also use if(array_key_exists('submit', $_POST))
是。你也可以使用if(array_key_exists('submit',$ _POST))