PDA

View Full Version : a way to ignore post data when page refreshes



cadaver
09-14-2006, 01:32 PM
it is a problem when you post a data from a form, press f5 and the data posts again. how to ignore it. let's see

make a hidden field with a value of time()
open a session
store the post time data in the session

when you hit F5 the page refreshes but the post time value remains same.
compare it with the session time value. if they are equal don't use the data.

when you click submit the time value changes and you will be able to gather information only from a submitted data not refreshed data

the example



<?php
/*start a session*/
session_start();

/*if there's no post set session time value to the recent time*/
if (!isset($_POST[time])) {$_SESSION[time]=time();}

/*if session time value equals to post time value it means this post is from a refresh*/
if ($_SESSION[time]==$_POST[time])
{echo 'Do not refresh the page';exit();}
/*if session time value is not equal to the post time value it means that it is from a submit*/
if ($_SESSION[time]<>$_POST[time])
{$_SESSION[time]=$_POST[time];}

echo'
<form method="post" action="">
<input type="hidden" name="time" value="'.time().'">
<input type="submit" value="Submit">
</form>
';
?>



have a good use

AbelaJohnB
09-14-2006, 05:29 PM
I also like to add the SELECT() on the last entry into the database and see if it matches the same users_id of the person submitting the new one. Unless you have a website that is not very active, chances are you are going to have someone else post between the time it takes the same person to post two submissions.

Basically just another method to deal with flood protection.

John B. Abela