PHP页面刷新时避免浏览器重新发布$ _POST? [英] PHP avoid browser reposting $_POST on page refresh?

查看:395
本文介绍了PHP页面刷新时避免浏览器重新发布$ _POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我可以使用什么技术来避免用户在刷新页面并选择再次提交时发表两次表单?

我在 regiter.php 内部创建了表单,并在 register.php中进行了处理。

1st 我可以在另一个文件中处理,例如 register_process.php 并重定向到register.php,但是我必须创建大约20个新页面并重新定位大量代码,我不想要这个选项。



2nd 我可以使用标题我不记得确切的技巧,但有一些糟糕的体验 - 用户在页面上看到过旧数据刷新后... ...



第三我可以将成功重定向到 dummy.php strong> dummy.php 跳回到 register.php ,那么即使它们刷新页面浏览器也不会重新发布,但是它并不能防止它们使用后退按钮 strong>并选择重新发布,我知道我可以过期页面​​,但是我发现对于我和其他用户看到页面过期错误的烦人体验。 $ b

4th 为每个表单使用一些唯一的访问键,一旦页面加载,将与表单一起使用,一旦使用不能重复使用,但是我种与该功能的逻辑斗争。如何知道密钥没有存储在MySQL DB中,我认为基于时间的访问不是很好,或者是因为一些用户可以在页面打开和表单提交之间花费很长时间。



I需要更多的建议,如何避免用户重新发布表单。

解决方案

试试这个:

 <?php 
session_start();
if(strcasecmp($ _ SERVER ['REQUEST_METHOD'],POST)=== 0){
$ _SESSION ['postdata'] = $ _POST;
header(Location:。$ _ SERVER ['PHP_SELF']。?。$ _ SERVER ['QUERY_STRING']);
出口;

if(isset($ _ SESSION ['postdata'])){
$ _POST = $ _SESSION ['postdata'];
unset($ _ SESSION ['postdata']);
}

这将基本上保存POST数据并导致浏览器重新请求一个GET请求。


I wonder what are the techniques i can use to avoid users to post form twice when they refresh page and chose submit again?

e.g. i have form inside regiter.php and process it as well inside register.php.

1st i could process in another file e.g. register_process.php and redirect to register.php, but then i have to create about 20 new pages and relocate a lot of code, i dont want that option.

2nd i could play with headers i dont remember exact trick but had some bad experience with that - users seen old data on page after refreshing it...

3rd i could just redirect upon success to some dummy.php and from dummy.php jump back to register.php then even if they refresh page browser would not re-post, however it does not protect against them using back button and choosing re-post, i know i could expire page, but i find that annoying experience for me and probably other users to see page expired error.

4th use some unique "access key" for each form once page loaded that will post with form and once used cannot be reused, however i kind of struggle with logics of that feature. how do know key was used without storing it in MySQL DB, i think time based accesis not great either because some users can take long between page open and form submit.

I need more suggestions how to avoid users reposing form again.

解决方案

Try this:

<?php
session_start();
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
  $_SESSION['postdata'] = $_POST;
  header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
  exit;
}
if( isset($_SESSION['postdata'])) {
  $_POST = $_SESSION['postdata'];
  unset($_SESSION['postdata']);
}

This will basically save the POST data and cause the browser to re-request as a GET request.

这篇关于PHP页面刷新时避免浏览器重新发布$ _POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆