php 为什么重新加载时设置了 POST['submit']?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8176845/
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
Why POST['submit'] is set when I reload?
提问by giannis christofakis
My application is a simple login page. When it fails, I print an error message. My question is, why when I reload the page the message is been printed again? How can I fix that? The code is working fine, I've made another php file executing the database check & connection.
我的应用程序是一个简单的登录页面。当它失败时,我会打印一条错误消息。我的问题是,为什么当我重新加载页面时,消息会再次打印?我该如何解决?代码工作正常,我制作了另一个执行数据库检查和连接的 php 文件。
<?php
require_once("include/database.php");
if(isset($_POST['submit'])) {
connect_bookstore(); // custom function
$spassword = sha1($_POST['password']);
$username = $_POST['username'];
if ( checkpassword($username,$spassword) ) { //custom function
header('Location:insert.php');
exit;
} else {
$message = "Login failed!";
}
}
?>
Inside the html body.
在 html 正文中。
<?php
if (isset($message)) {
echo $message;
}
?>
回答by Kai Qing
<?php
session_start();
require_once("include/database.php");
if(isset($_POST['submit'])) {
connect_bookstore(); // custom function
$spassword = sha1($_POST['password']);
$username = $_POST['username'];
if ( checkpassword($username,$spassword) ) { //custom function
header('Location:insert.php');
exit;
} else {
$_SESSION['message'] = "Login failed!";
header('location: /yourfile.php');
exit;
}
}
if(isset($_SESSION['message']))
{
echo $_SESSION['message'];
unset($_SESSION['message']);
}
?>
Fundamentally, yes, post/redirect/get... but sometimes a simple explanation is better.
从根本上说,是的,发布/重定向/获取...但有时一个简单的解释会更好。
I use sessions to store flash messages, then display them like this.
我使用会话来存储 Flash 消息,然后像这样显示它们。
回答by Qasim
Thats because you are resending the same POST data when you refresh, if you do a GET request you will notice in the URL your parameters that you are passing are there, so if you refresh those parameters are once again sent. Same thing with POST.
那是因为您在刷新时重新发送相同的 POST 数据,如果您执行 GET 请求,您会在 URL 中注意到您传递的参数在那里,因此如果您刷新这些参数将再次发送。与 POST 相同。
回答by SLaks
When you reload the page, the browser will send the same request that it sent for theoriginal page.
当您重新加载页面时,浏览器将发送它为原始页面发送的相同请求。
You want a POST-Redirect-GET.
你想要一个POST-Redirect-GET。