如何避免重复,当用户点击“&刷新QUOT;他的浏览器的按钮? [英] How to avoid duplicate when a user click the "refresh" button of his browser?

查看:102
本文介绍了如何避免重复,当用户点击“&刷新QUOT;他的浏览器的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我问这个问题,然后点击张贴您的问题,并留在当前页面。现在,我可以点击我的浏览器的刷新按钮来查看新的答案。但我发现,在我的网站,如果我点击刷新按钮,重复的问题将予以公布。如何避免这个问题?
我使用PHP。


解决方案

这是一个 POST 请求后常见的做法,重定向到同一个页面,以避免这个问题。

比方说,你是在 /ask_question.php

您开放<形式为GT; 标记可能是这样的:

 <形式的行动=/ ask_question.php的方法=后>

现在,在你的 /ask_question.php 做这样的事情:

 如果(使用isset($ _ POST ['新问题'])){
    //在这里做你的处理    //在成功:
    标题(位置:/ask_question.php');
    出口(); // END请求
}

更新,以一个有效的 $ _ POST 请求被处理后,只有重定向的这是非常重要的。我测试名为表单域新问题,但你应该使用具有成为了 $ _ POST

成功

此处理他们发布的数据,然后成功,重定向回同一页。唯一的区别现在是,当他们点击刷新,没有信息将被公布。

注意只要确保没有什么是回声'之前的头编出来呼叫。

For example, I ask this question and click "Post your question" and stay in the current page. Now I may click the "refresh" button of my browser to see new answers. But I find that on my website, if I click the "refresh" button, a duplicated question will be published. How to avoid this problem? I am using PHP.

解决方案

It is common practice after a POST request, to redirect to the same page to avoid this problem.

Lets say you are on /ask_question.php

Your opening <form> tag might look like this:

<form action="/ask_question.php" method="post">

Now, in your /ask_question.php do something like this:

if( isset($_POST['new-question'])){
    // Do your processing here

    // On success:
    header('Location: /ask_question.php');
    exit(); // End the request
}

Update It is important to only redirect after a valid $_POST request has been handled. I test for a form field named new-question but you should use any form field name that has to be present for the $_POST to succeed

This processes their posted data, then on success, redirects back to the same page. The only difference now, is that when they click refresh, no information will be posted.

Note Just make sure nothing is echo'ed out prior to the header call.

这篇关于如何避免重复,当用户点击&ldquo;&刷新QUOT;他的浏览器的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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