Refresh POST problem

arcdogl
arcdogl used Ask the Experts™
on
Hello,

Part of my system is used to log user events, so for example a user will click a button to say they have started a process and then click again to say they have stopped. I encounter a problem when someone refreshes the page or browses back to a page and the event is submitted again. The way I have stopped this in the past is if the event POSTed is the current event then it must be an accident so ignore it - for example if you click to start but its already in a started state.

Is there a better way of doing this as the way i'm doing it now seems a bit messy?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
Hellom, arcdogl, Limited Member and welcome to EE.  One helpful thought for you, going forward, is that Experts answer questions for points here.  If you look at the number of 500-point posts in this zone, you will see that a 75-point post may not get as much attention - simple economics.

If I understand your situation correctly, you have some dependence on "stateful" information that is disrupted by browser window mechanics.  This is a common situation when one relies on sessions to keep the state of the client machine.  A variation on this theme occurs when your client has more than one browser window open - the sessions can persist even though the server-side logic would suggest that the session should disappear after the window is closed.

Suggest you consider putting your information into a data base and letting each page load bring in a new copy of the data.  Then you can have timestamp-tracking and other technologies to help keep track of things.

Best regards, ~Ray

Author

Commented:
Hi Ray,

I apreciate your comment about the points so I've increased them slightly.

my data is stored in a database and pulled in each time but when a user submits a form, for example to update the database, and the form is posted, if they then refresh the page it is posted again and unless I catch it it's entered into the database again. ideally I would prefer so that even if the user pressed refresh the form wasn't reposted at all.

Most Valuable Expert 2011
Top Expert 2016
Commented:
OK, now I understand better.  Please have a look at this code snippet and see if it makes sense to you.
<?php // RAY_multi_submit.php
error_reporting(E_ALL);
 
 
// ALWAYS START SESSION ON EVERY PAGE
session_start();
 
 
// PREVENT MULTIPLE SUBMISSIONS DUE TO REPEATED CLICKS ON SUBMIT BUTTON
// EXAMPLE: if ( multi_submit() ) { /* error */ } else { /* normal processing */ }
function multi_submit($type = "post")
{
	$string = "";
	$input_array = ($type == "get") ? $_GET : $_POST;
	foreach ($input_array as $val) { $string .= $val; }
	$string = md5($string);
 
	if (!isset($_SESSION["_multi_submit"]))
	{
		$_SESSION['_multi_submit'] = $string;
		return FALSE;
	}
 
	if ($_SESSION['_multi_submit'] === $string)
	{
		return TRUE;
	} else
	{
		$_SESSION['_multi_submit'] = $string;
		return FALSE;
	}
}
 
 
 
 
// USE CASE FOR THE multi_submit() FUNCTION
if (!empty($_POST))
{
    if (multi_submit())
    {
        die("ALREADY GOT THAT");
    }
}
// END OF PHP
?>
<form method="post">
ENTER SOMETHING, THEN REENTER IT
<input name="foo" />
<input type="submit">
</form>

Open in new window

Author

Commented:
Hi Ray,

Sorry it's took me so long to reply.

Yes, that seems a much nicer way to do it. so its merging the full POST or GET string, encoding it and storing it in the session.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points - it's a good question! ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial