0

On my page, after the user has submitted a code, the script makes a new record in the database. If after he has submitted the code the user hits refresh, it will make a duplicate record in the database. If he hits refresh 10 times, it will make 10 records with the same data...

Is there some way I can make the page not to refresh for the second time?? Or to limit the number of refreshes the page can get? What solutions can be applied to avoid this situation?

Thank you in advance.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Merlin
  • 179
  • 4
  • 11

5 Answers5

0

Before adding the record into the data table, use select query to check the data is alreary exists or not in the data table and redirect the page to a new page, say thank you or confirmation of insertion.

Select id from TABLE where data='".$_POST["data"]."';
....
....
if(empty($rs["id"])){
// insert the data in table;
}else{
// do edit or skip to redirect
}

and Rediret

header("location:thankyou.php");
exit();
Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
0

It sounds like the PHP script that produces the form is the same one that processes it. You may want to consider setting up a separate PHP script to handle the form. For example:

<form action="action_handler.php" method="post">

Then, at the end of action_handler.php, you would redirect to the original page as follows:

header('Location: original_page.php');
David Jones
  • 10,117
  • 28
  • 91
  • 139
0

After the records are saved in the database, use header() and redirect to the same page.

Check the answer to this question, maybe you can use the same code.

Community
  • 1
  • 1
KaeL
  • 3,639
  • 2
  • 28
  • 56
0

Simple one is unset($_POST["data"]) after inserting it to database or try header('location:.')

Chetak
  • 3
  • 4
0

When dealing with POST-data you should always employ a POST/REDIRECT/GET setup (Wikipedia. This way, when a user refreshes the destination page, nothing really happens besides a refresh.

The best way for a redirect method is to use status code 303 (HTTP Status Codes), which tells the browser that the POST-data has been received, processed and that the user should go to a new page for the 'response'. When a user hits 'back' he or she should get a message that the page is no longer available or valid (this disabling rePOSTing via that route).

You can use this example to redirect to another page (fairly generic example, but I have baseline code I could use):

header("HTTP/1.1 303 See Other");
header("Location: http://www.yourdomain.com/desitnation.php");
KilZone
  • 1,580
  • 9
  • 20