我有一个向SQL表提交文本的简单表单。问题在于,在用户提交文本之后,他们可以刷新页面并再次提交数据,而无需再次填写表单。我可以在文本提交后将用户重定向到另一个页面,但我希望用户停留在同一页面上。
我记得我读过一些关于给每个用户一个唯一的会话id,并将其与另一个值进行比较,这解决了我所遇到的问题,但我忘记了它在哪里。
我有一个向SQL表提交文本的简单表单。问题在于,在用户提交文本之后,他们可以刷新页面并再次提交数据,而无需再次填写表单。我可以在文本提交后将用户重定向到另一个页面,但我希望用户停留在同一页面上。
我记得我读过一些关于给每个用户一个唯一的会话id,并将其与另一个值进行比较,这解决了我所遇到的问题,但我忘记了它在哪里。
当前回答
我还想指出,您可以使用javascript方法window.history.replaceState来防止在刷新和返回按钮时重新提交。
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
这里的概念证明:https://dtbaker.net/files/prevent-post-resubmit.php(链接不再工作)
我仍然推荐Post/Redirect/Get方法,但这是一个新颖的JS解决方案。
其他回答
基本上,你需要重定向出该页面,但它仍然可以使一个问题,而你的互联网缓慢(重定向头从服务器端)
基本场景示例:
点击提交按钮两次
解决方法
客户端 一旦客户端点击它,禁用提交按钮 如果你使用Jquery: Jquery.one PRG模式 服务器端 使用基于差分的哈希时间戳/发送请求时的时间戳。 Userequest令牌。当主进程加载完成时,分配一个临时请求标记,如果重复则忽略该标记。
正如其他人所说,不可能不使用post/redirect/get。但与此同时,在服务器端做你想做的事情也很容易。
在POST页面中,只需验证用户输入,但不对其进行操作,而是将其复制到SESSION数组中。然后再次重定向到主提交页面。您的主提交页面首先检查您正在使用的SESSION数组是否存在,如果存在,则将其复制到本地数组中并取消设置。从那里你可以采取行动。
这样你只需要做一次主要的工作,就能完成你想做的事情。
我寻找解决方案,以防止在一个巨大的项目之后再次提交。 代码高度工作与$_GET和$_POST和我不能改变表单元素的行为没有不可预见的错误的风险。 这是我的代码:
<!-- language: lang-php -->
<?php
// Very top of your code:
// Start session:
session_start();
// If Post Form Data send and no File Upload
if ( empty( $_FILES ) && ! empty( $_POST ) ) {
// Store Post Form Data in Session Variable
$_SESSION["POST"] = $_POST;
// Reload Page if there were no outputs
if ( ! headers_sent() ) {
// Build URL to reload with GET Parameters
// Change https to http if your site has no ssl
$location = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// Reload Page
header( "location: " . $location, true, 303 );
// Stop any further progress
die();
}
}
// Rebuilt POST Form Data from Session Variable
if ( isset( $_SESSION["POST"] ) ) {
$_POST = $_SESSION["POST"];
// Tell PHP that POST is sent
$_SERVER['REQUEST_METHOD'] = 'POST';
}
// Your code:
?><html>
<head>
<title>GET/POST Resubmit</title>
</head>
<body>
<h1>Forms:</h1>
<h2>GET Form:</h2>
<form action="index.php" method="get">
<input type="text" id="text_get" value="test text get" name="text_get"/>
<input type="submit" value="submit">
</form>
<h2>POST Form:</h2>
<form action="index.php" method="post">
<input type="text" id="text_post" value="test text post" name="text_post"/>
<input type="submit" value="submit">
</form>
<h2>POST Form with GET action:</h2>
<form action="index.php?text_get2=getwithpost" method="post">
<input type="text" id="text_post2" value="test text get post" name="text_post2"/>
<input type="submit" value="submit">
</form>
<h2>File Upload Form:</h2>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit" value="submit">
</form>
<h1>Results:</h1>
<h2>GET Form Result:</h2>
<p>text_get: <?php echo $_GET["text_get"]; ?></p>
<h2>POST Form Result:</h2>
<p>text_post: <?php echo $_POST["text_post"]; ?></p>
<h2>POST Form with GET Result:</h2>
<p>text_get2: <?php echo $_GET["text_get2"]; ?></p>
<p>text_post2: <?php echo $_POST["text_post2"]; ?></p>
<h2>File Upload:</h2>
<p>file:
<pre><?php if ( ! empty( $_FILES ) ) {
echo print_r( $_FILES, true );
} ?></pre>
</p>
<p></p>
</body>
</html><?php
// Very Bottom of your code:
// Kill Post Form Data Session Variable, so User can reload the Page without sending post data twice
unset( $_SESSION["POST"] );
它只能避免$_POST的重新提交,而不是$_GET的重新提交。但这是我需要的行为。 重新提交的问题不工作的文件上传!
我还想指出,您可以使用javascript方法window.history.replaceState来防止在刷新和返回按钮时重新提交。
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
这里的概念证明:https://dtbaker.net/files/prevent-post-resubmit.php(链接不再工作)
我仍然推荐Post/Redirect/Get方法,但这是一个新颖的JS解决方案。
$_POST['submit']变量在页面初始加载时不存在,并且只有在以下条件为真时才可以运行curl。
if($_POST['submit'] == "submit"){
// This is where you run the Curl code and display the output
$curl = curl_init();
//clear $post variables after posting
$_POST = array();
}