Announcement

  •  » Engine
  •  » advice about preventing form resubmission in extensions

#1 2021-02-19 20:56:55

eliz82
Member
Romania
2016-04-27
281

advice about preventing form resubmission in extensions

I'm working on an extension. It has an admin.php and admin.tpl with a html FORM. so admin.php make both data submission and POST data reception and processing ... like I have seen most of the extensions are doing.

I observed in my extension that if I refresh the page after the successful POST, the data is processed again.
This is a well know issue for developers:
https://en.wikipedia.org/wiki/Post/Redirect/Get
https://stackoverflow.com/questions/632 … d-f5-ctrlr

I wonder are they any know methods/functions already implemented in Piwigo that can prevent this?
I have studied some plugins code and didn't find anything.

Last edited by eliz82 (2021-02-20 16:30:10)

Offline

 

#2 2021-02-19 21:59:43

jnashpiwigo
Piwigo Team
2014-10-21
254

Re: advice about preventing form resubmission in extensions

Pseudo code:

On submission, check for existence of form field ($_GET / $_POST)
If exists, then set a new 'hidden' form field in form
On 'refresh/resubmission' check for this new, hidden form field 
If exists abort unwanted action

This is just one way I've handled these things - I'm sure there are much more and much better methods.

* Disclaimer:  I am not a programmer, but sometimes I play one on the Internet.

Offline

 

#3 2021-02-20 11:10:02

eliz82
Member
Romania
2016-04-27
281

Re: advice about preventing form resubmission in extensions

so the code of admin.php should look something like this:

Code:

$repost = false;
if (isset($_POST['submit'])) {
  if (isset($_POST['repost']))
    $repost = true;
  }
  else {
    //do your normal data processing
  }
}

$template->assign(
  array(
    'MY_PREFIX_REPOST' => $repost,
    )
  );

and admin.tpl

Code:

<form method="post">
  {if $MY_PREFIX_REPOST}
  <input type="hidden" name="repost"> 
  {/if}
  <button name="submit" type="submit">Start</button>
</form>

interesting method, thanks. I must test this to see if it works. do you have a plugin in in which you implemented this method?

Offline

 

#4 2021-02-20 17:12:25

jnashpiwigo
Piwigo Team
2014-10-21
254

Re: advice about preventing form resubmission in extensions

That looks about right to me. 

I don't have any plugins, but have used this method in many PHP forms.

Offline

 

#5 2021-03-17 10:02:45

eliz82
Member
Romania
2016-04-27
281

Re: advice about preventing form resubmission in extensions

I have found a thread with similar issues
https://piwigo.org/forum/viewtopic.php?id=28865

Offline

 

#6 2021-03-17 10:22:32

flop25
Piwigo Team
2006-07-06
7037

Re: advice about preventing form resubmission in extensions

PS: don't forget to use the Piwigo token and check_input_parameter :)


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 
  •  » Engine
  •  » advice about preventing form resubmission in extensions

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact